应用场景:频繁触发某一事件,频繁执行DOM操作、资源加载等重行为;
function debounce(fun, delay) {
return function () {
var that = this;
clearTimeout(fun.mark);
fun.mark= setTimeout(function () {
fun.call(that)
}, delay)
}
}
function AJAX() {
$.ajax({
url:"url"
}).done(function (res) {
console.log(res);
})
}
var debounceAjax = debounce(AJAX, 500);
$("#searchInput").on("input", function () {
debounceAjax();//搜索框搜索,实时更新数据
});