同步-阻塞式调用,等待执行
function waitFive(name,function_name){
var pus=0;
var currentDate=new Date();
while(pus<5000){
var now=new Date();
pus=now -currentDate;
}
function_name(name);
}
function echo(name){
console.log(name);
}
waitFive('fork',echo);
回调概念-回调是一个异步等效的功能。
1不直接返回执行结果
2通过函数的参数传递回调函数
3在回调函数中返回执行结果
var test=function(name,callback){
callback(name);
}
var callback=function(name){
echo 'call back';
return name;
}
test('fork',callback);
同步阻塞:
主干函数调用异步方法时,使用异步调用,由系统内核执行回调函数,此时主干执行其他操作,当回调函数执行完后
返回结果,此时由主干处理回调
function Person(){
this.think=function(callback){
setTimeout(function(){console.log('thinking!'),callback()},5000);
}
this.answer=function(){
console.log('I am answering other question');
}
}
var person=new Person();
person.think(function(){
console.log('thinking 2 seconds,get the right answer!');
})
person.answer();
缺点:
难以阅读
编码困难
调试复杂
408

被折叠的 条评论
为什么被折叠?



