/** Frequency control when a continuous call is returned from a function, the FN execution frequency is limited to the number of times per time * @param fn {function} needs to call the function * @param delay {Number} delay time, in milliseconds * @param immed The function that iate {bool} passes a false binding to the immediate parameter executes first, not after delay. * @param debounce {bool} The function that passes the false binding to the immediate parameter executes first, not after delay. debounce* @return {function} is actually called*/ varThrottle =function(FN, delay, immediate, debounce) {varCurr = +NewDate (),//Current EventsLast_call = 0, Last_exec= 0, Timer=NULL, diff,//Time DifferenceContext//Contextargs, exec=function() {last_exec=Curr; Fn.apply (context, args); }; return function() {Curr= +NewDate (); Context= This, args=arguments, diff= Curr-(debounce? last_call:last_exec)-delay; Cleartimeout (timer); if(debounce) {if(immediate) {Timer=setTimeout (exec, delay); } Else if(diff >= 0) {exec (); } } Else { if(diff >= 0) {exec (); } Else if(immediate) {Timer= SetTimeout (EXEC,-diff); }} Last_call=Curr; }};
var setval = function () { var mydate = new Date (); var s = Mydate.getseconds (); var ms = Mydate.getmilliseconds (); document.getElementById ( IPT). value=s+ ":" +MS;} The following two binding methods are available, especially jquery binding. Note $ ("#btn") [0]
// document.getElementById ("btn"). onclick = throttle (function () {setval ();},1000,true,false);
$ ("#btn") [0].onclick = Throttle (function() {setval ();},1000,true,false);
JS Throttle Function Throttle