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

javascript function limit calling code_javascript skills

WBOY
Release: 2016-05-16 18:26:53
Original
1032 people have browsed it

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
]
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template