JS Throttle Function Throttle

Source: Internet
Author: User

/** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.