普通未防抖款code
var textElement = document.getElementById('test'); var timer; textElement.oninput = function(){ console.log('造吧,木有防抖效果'); }
普通防抖款blog
var textElement = document.getElementById('test'); var timer; textElement.oninput = function(){ console.log('oninput事件'); if(timer){ clearTimeout(timer); } timer = setTimeout(function(){ timer = null; console.log('添加防抖效果以後的操做') },2000) }
封裝防抖款事件
function antishake(fn,delay){ return function(){ if(timer){ clearTimeout(timer); } timer = setTimeout(function () { timer = null; fn(); },delay); } } function business(){ console.log('添加防抖效果以後的操做') } textElement.oninput = function(){ console.log('oninput事件') antishake(business,800)(); }