Function:
Copy Code code as follows:
Function Throttle (FN,MS) {
var last = (new Date ()). GetTime ();
Return (function () {
var now = (new Date ()). GetTime ();
if (Now-last > ms) {
last = Now;
Fn.apply (this, arguments);
}
});
}
PARAMETER fn: Incoming function/method
Parameter MS: the interval (in milliseconds) for each function call, such as input 2000, the function does not repeat in 2 seconds.
An example of initialization
Copy Code code as follows:
document.getElementById (' Pop '). onclick = throttle (function () {
alert (this.id);
},2000)
The scope is set to the caller itself
Fn.apply (this, arguments);
Examples
<! DOCTYPE html> <ptml xmlns= "http://www.w3.org/1999/xhtml" > <HEAD> <TITLE> New Flowers </TITLE&G T </HEAD> <style type= ' text/css ' > #pop {width:100px; height:100px; Background: #ccc; } </style> <BODY> <div id= "Pop" > virtual bit has been waiting </div> </BODY> </HTML> <script type= ' text /javascript ' > Function throttle (fn,ms) {var last = (new Date ()). GetTime (); Return (function () {var now = (new Date ()). GetTime (); if (Now-last > ms) {last = now; Fn.apply (this, arguments); } }); } document.getElementById (' Pop '). onclick = throttle (function () {alert (this.id); },2000) </script>
[ctrl+a All selected note: If you need to introduce external JS need to refresh to perform]