A question I encountered in the interview today has not been figured out for a long time and has been despised.
requires that the
1 and poll methods receive two functions, checkStatus and callback, where the return value of checkStatus is of Boolean type, and true or false,callback is a callback function.
2. If checkStatus returns true, to execute callback, otherwise continue to execute checkStatus, but there will be a delay.
3. Polling time needs to increase constantly. After the first execution of checkStatus returns false, 1000ms is required to execute it for the second time, and the time of each subsequent execution will be increased by 1.5 times.
4, must not use global variables .
function time1(){
var t = 1000;
function time2(){
t *= 1.5;
return t;
}
return time2;
}
var time3 = time1();
function poll(checkStatus,callback){
if(checkStatus()){
callback();
}else{
setTimeout(function(){
console.log(new Date());
poll(checkStatus,callback);
},time3());
}
}
function checkStatus(){
return 0;
}
function callback(){
console.log("callback");
}
poll(checkStatus,callback);
that"s all I can think of, but I still use global variables to solve!