Home  >  Article  >  Web Front-end  >  javascript function limit calling code_javascript skills

javascript function limit calling code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:26:53978browse

Function:

Copy code The code is 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);
}
});
}

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

Copy code The code is as follows:

document.getElementById('pop').onclick = throttle(function (){
alert(this.id);
},2000)


scope setting For the caller itself
fn.apply(this, arguments);

Examples

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]
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