gpt4 book ai didi

javascript - 带回调的 jQuery 简单函数

转载 作者:行者123 更新时间:2023-11-29 16:24:49 25 4
gpt4 key购买 nike

我正在尝试编写一个函数,该函数将执行某些操作,然后在完成后绑定(bind)回调,我想在初始化 js 函数时指定回调...

function hello(arg1,arg2,callback){
alert(arg1 + arg2);

callback;
}

hello('hello','world',function callback(){
alert('hey 2);
});

很抱歉问了一个平庸的问题,我正在尝试了解如何将回调函数传递给函数。

最佳答案

您需要像调用其他函数一样调用该函数:

function hello(arg1,arg2,callback){
alert(arg1 + arg2);

callback();
}

hello('hello','world',function callback(){
alert('hey 2);
});

请注意,在 JavaScript 中,有更好的方法来执行该回调,例如 .apply()call()但只有当您计划在回调中使用 this 关键字时才需要这样做。

关于javascript - 带回调的 jQuery 简单函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7126926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com