Last active
August 29, 2015 13:57
-
-
Save worldask/9543435 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from http://javascript.ruanyifeng.com/bom/engine.html#toc9 | |
// 防抖动,只有当两次触发之间的时间间隔大于事先设定的值,这个新函数才会运行实际的任务 | |
function debounce(fn, delay){ | |
var timer = null; // 声明计时器 | |
return function(){ | |
var context = this, args = arguments; | |
clearTimeout(timer); | |
timer = setTimeout(function(){ | |
fn.apply(context, args); | |
}, delay); | |
}; | |
} | |
// from http://javascript.ruanyifeng.com/bom/engine.html#toc10 | |
// 循环调用setTimeout模拟了setInterval,防止setInterval阻塞 | |
function interval(func, wait){ | |
var interv = function(w){ | |
return function(){ | |
setTimeout(interv, w); | |
func.call(null); | |
} | |
}(wait); | |
setTimeout(interv, wait); | |
} | |
// http://javascript.ruanyifeng.com/jquery/deferred.html#toc12 | |
// 改写setTimeout方法,让其返回一个deferred对象 | |
function doSomethingLater(fn, time) { | |
var dfd = $.Deferred(); | |
setTimeout(function() { | |
dfd.resolve(fn()); | |
}, time || 0); | |
return dfd.promise(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment