1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
function throttle(callback, timmer) { let isFirst = true; let execDate = +new Date(); let throttleId; return function () { if (isFirst) { callback(); execDate = +new Date(); isFirst = false; } else { const currentDate = +new Date(); if (currentDate - execDate >= timmer) { callback(); execDate = + new Date(); } else { if (throttleId) { clearTimeout(throttleId) }; const timeWait = execDate + timmer - (+new Date()); throttleId = setTimeout(() => { callback(); execDate = + new Date(); }, timeWait) } } } }
|