functionthrottleEvelator(){ var work = function(){ console.log("let's go"); }; var timeout = 5; var begin = 0; var now = 0; returnfunction(){ now = newDate().getTime()/1000; if (now - begin > timeout) { begin = now; setTimeout(function(){ work(); },timeout*1000); } } }
functionthrottleEvelator(){ var work = function(){ console.log("let's go"); }; var timeout = 5; var begin; var now = 0; var timer = null; returnfunction(){ now = newDate().getTime()/1000; begin = begin || now; if (now - begin < timeout) { begin = now; clearTimeout(timer); timer = setTimeout(function(){ work(); },timeout*1000); }else{ begin = now; timer = setTimeout(function(){ work(); },timeout*1000); } } }
functionthrottleEvelator(){ var work = function(){ console.log("let's go"); }; var timeout = 5; var begin; var now = 0; var timer = null; var flag = true; returnfunction(){ console.log(flag); // flag == true 直接设置定时器 // flag == false 清除定时器再设置 if (flag) { flag = false; timer = setTimeout(function(){ work(); flag = true; },timeout*1000); }else{ clearTimeout(timer); timer = setTimeout(function(){ work(); flag = true; },timeout*1000); } } }