Function:
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);
}
});
}
parameters fn: Incoming function/method
Parameter ms: The interval (in milliseconds) between each function call. If you enter 2000, the function will not be triggered repeatedly within 2 seconds.
Attached is an initialization example
document.getElementById('pop').onclick = throttle(function (){
alert(this.id);
},2000)
scope setting For the caller itself
fn.apply(this, arguments);
Examples
]
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn