js闭包

判断checkstatus的状态,为false则隔1.5秒的倍数再次判断checkstatus,只有当checkstatus为true时执行callback回调
var checkstatus = false;
var callback = function() {
    var now = new Date(); 
    console.log(now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + ' callback run~~');
};
function check(checkstatus, callback){
    time(0, checkstatus, callback)
}
function time(i, checkstatus, callback) {
    setTimeout(function(i, checkstatus, callback) {
        var now = new Date();
        console.log(now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + ' output:' + i);
        if (i == 5) {
            checkstatus = true;
        }
        if ( !checkstatus ) {
            time(++i, checkstatus, callback);
        } else {
            callback();
        }
    }, i * 1500, i, checkstatus, callback);
}

check(checkstatus, callback);

你可能感兴趣的:(js闭包)