: Throttle function will be performed after the second ever after a trigger function, perform for the first time, only the execution cycle is greater than the set
1 / * 2 throttle function: fn: function to be throttled, delay: predetermined time . 3 * / . 4 function Throttle (Fn, Delay) { . 5 // time recording function of the starting time . 6 var lastTime = 0 . 7 return function () { . 8 // record the time of the current function triggered . 9 var nowtime = new new a Date (). the getTime () 10 // when the current time minus the first execution time is greater than the specified interval will be allowed to trigger the function 11 IF (nowtime - lastTime> Delay) { 12 is // bind this point 13 is fn.call ( this ) 14 // synchronization time 15 lastTime = nowtime 16 } . 17 } 18 is }
Function Image Stabilization: continue to trigger a function, within the specified time so that only the last time to take effect, not in front of the entry into force
. 1 function Debounce (Fn, Delay) { 2 var Timer = null . 3 // Clear time delay . 4 return function () { . 5 the clearTimeout (Timer) . 6 // reset a new delay . 7 Timer = the setTimeout ( () => { . 8 fn.call ( the this ) . 9 }, Delay); 10 } . 11 }
More face questions go to GitHub https://github.com/bettersong/interview