回调函数示例

回调函数示例,第1张

回调函数示例

当您将函数作为参数传递时,它称为回调函数,并且当您通过此回调函数返回值时,该值就是所传递函数的参数。

function myFunction(val, callback){    if(val == 1){        callback(true);    }else{        callback(false);    }}myFunction(0, //the true or false are passed from callback() //is getting here as bool// the anonymous function below defines the functionality of the callbackfunction (bool){    if(bool){        alert("do stuff for when value is true");    }else {        //this condition is satisfied as 0 passed        alert("do stuff for when value is false");    }});

基本上,callbacks()用于异步概念。在特定事件上调用它。

myFunction
也是回调函数。例如,它发生在单击事件上。

document.body.addEventListener('click', myFunction);

这意味着,首先将动作分配给其他功能,然后再考虑一下。满足条件时将执行该 *** 作。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5171881.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存