js回调函数

回调一词经常见,到底是什么意思呢?

A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.

还是英文解释的比较贴切。

再来一个例子:

<html> 
<head> 
<title>回调函数(callback)</title> 
<script language="javascript" type="text/javascript"> 
    function a(callback){  
        console.info("我是parent函数a!");
        console.info("调用回调函数callback开始");
        callback(); 
        console.info("调用回调函数callback结束");
    } 
    function b(){ 
    //alert("我是回调函数b"); 
    console.info("我是回调函数b");
    }
    function c(){ 
    //alert("我是回调函数c"); 
     console.info("我是回调函数c");
    } 

    function test(){ 
        a(b); 
        a(c); 
    } 
</script> 
</head> 
 
<body> 
    <h1>学习js回调函数</h1> 
    <button onClick=test()>click me</button> 
    <p>测试调用回调函数的顺序</p> 
</body>  
</html> 

执行结果:

原文地址:https://www.cnblogs.com/fanyong/p/2992554.html