The function is executed at the time interval of throttle.
Debounce time interval T Nejo the event again, the timer is re-timed until the stop time is greater than or equal to T to execute the function.
1. Simple implementation of throttle function
functionThrottle (FN, threshhold, scope) {threshhold|| (threshhold = 250); varLast , timer; return function () { varContext = Scope | | This; varnow = +NewDate (), args=arguments; if(Last && now-last + threshhold < 0) { //Hold on to itcleartimeout (Defertimer); Timer= SetTimeout (function() { last=Now ; Fn.apply (context, args); }, threshhold); } Else{ Last=Now ; Fn.apply (context, args); } };}//Calling Methods$ (' body '). On (' MouseMove ', throttle (function(event) {Console.log (' Tick ');}, 1000));
2. Simple implementation of debounce function
functiondebounce (FN, delay) {varTimer =NULL; return function () { varContext = This, args=arguments; Cleartimeout (timer); Timer= SetTimeout (function() {fn.apply (context, args); }, delay); };}//Calling Methods$ (' Input.username '). KeyPress (Debounce (function(event) {//Do the Ajax request}, 250));
Transferred from: http://www.cnblogs.com/fsjohnhuang/p/4147810.html
JavaScript function throttling (throttle) and function shake (debounce)