JavaScript回调函数调用后 [英] JavaScript Callback after calling function

查看:198
本文介绍了JavaScript回调函数调用后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,让我说我有这个功能:

  function a(message){
alert ;
}

我希望在警报窗口显示后有回调。像这样:

  a(Hi。,function(){}); 

我不知道如何在我调用的函数里面有一个回调。 p>

(我只是以警告窗口为例)



谢谢!

解决方案

回调函数没有特殊的语法,只需传递回调函数并在函数内调用即可。

  function a(message,cb){
console.log(message); //日志到最近浏览器的控制台
cb();
}

a(Hi。,function(){
console.log(After hi ...);
});

输出:

 code> Hi。 
After hi ...


Ok so lets say I have this function:

function a(message) {
alert(message);
}

And I want to have a callback after the alert window is shown. Something like this:

a("Hi.", function() {});

I'm not sure how to have a callback inside of the function I call like that.

(I'm just using the alert window as an example)

Thanks!

解决方案

There's no special syntax for callbacks, just pass the callback function and call it inside your function.

function a(message, cb) {
    console.log(message); // log to the console of recent Browsers
    cb();
}

a("Hi.", function() {
    console.log("After hi...");
});

Output:

Hi.
After hi...

这篇关于JavaScript回调函数调用后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆