<HTML>
<body>
</body>
<script>
let f = debounce(alert, 1000);
//setting default value of time
function debounce(f, ms) {
let isCooldown = false;
return function() {
if (isCooldown) return; //ignore the call and end the function
//if its false, then call the f(=alert)
f.apply(this, arguments);
isCooldown = true;//flag is on to ignore future calls
setTimeout(() => isCooldown = false, ms);
//set timeout to turn flag to be false later
};
}
f(1); // runs immediately
f(2); // ignored
setTimeout( () => f(3), 100);
// ignored ( only 100 ms passed )
setTimeout( () => f(4), 1100); // runs
setTimeout( () => f(5), 1500);
// ignored (less than 1000 ms from the last run)
</script>
</HTML>
<body>
</body>
<script>
let f = debounce(alert, 1000);
//setting default value of time
function debounce(f, ms) {
let isCooldown = false;
return function() {
if (isCooldown) return; //ignore the call and end the function
//if its false, then call the f(=alert)
f.apply(this, arguments);
isCooldown = true;//flag is on to ignore future calls
setTimeout(() => isCooldown = false, ms);
//set timeout to turn flag to be false later
};
}
f(1); // runs immediately
f(2); // ignored
setTimeout( () => f(3), 100);
// ignored ( only 100 ms passed )
setTimeout( () => f(4), 1100); // runs
setTimeout( () => f(5), 1500);
// ignored (less than 1000 ms from the last run)
</script>
</HTML>
댓글
댓글 쓰기