一,回调函数概念
JS API:A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.
把函数当做一个参数传递,能够更加灵活地调用函数。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>回调函数(callback)</title>
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script language="javascript" type="text/javascript">
var f;
function d(){
alert("我是Jquery定义的函数");
}
var e = function(){
alert("我也是Jquery定义的函数");
}
function a(callback) {
alert("我是parent函数a!");
alert("调用回调函数");
d();//起作用
if (typeof callback === "function"){
alert(callback);
}
callback();
}
function b(){
alert("我是回调函数b");
e();
}
function c(){
alert("我是回调函数c");
d();
f();
}
function test() {
a(b);
a(c);
}
$(function(){
f = function(){
alert("我是回调函数f");
}
});
</script>
</head>
<body>
<h1>学习js回调函数</h1>
<button onClick=test()>click me</button>
<p>应该能看到调用了两个回调函数</p>
</body>
</html>