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

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

问题描述

好吧,假设我有这个功能:

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)

谢谢!

推荐答案

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

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...");
});

输出:

Hi.
After hi...

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

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