Created
May 27, 2020 04:21
-
-
Save reygreen1/009f0aec0c87f6256d6776a2f589fa83 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/pupudim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<button id="btn">click</button> | |
<script id="jsbin-javascript"> | |
function throttle(fn, delay) { | |
fn.canRun = true; | |
return function(...args) { | |
if(fn.canRun) { | |
let _this = this; | |
fn.canRun = false; | |
fn.call(_this, ...args); | |
setTimeout(() => { | |
fn.canRun = true; | |
}, delay); | |
} | |
} | |
} | |
var throttleConsole = throttle((val) => { | |
console.log(`${val}:` + new Date()); | |
}, 1000); | |
let i=0; | |
document.getElementById('btn').addEventListener('click', () => { | |
throttleConsole(i++); | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function throttle(fn, delay) { | |
fn.canRun = true; | |
return function(...args) { | |
if(fn.canRun) { | |
let _this = this; | |
fn.canRun = false; | |
fn.call(_this, ...args); | |
setTimeout(() => { | |
fn.canRun = true; | |
}, delay); | |
} | |
} | |
} | |
var throttleConsole = throttle((val) => { | |
console.log(`${val}:` + new Date()); | |
}, 1000); | |
let i=0; | |
document.getElementById('btn').addEventListener('click', () => { | |
throttleConsole(i++); | |
});</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function throttle(fn, delay) { | |
fn.canRun = true; | |
return function(...args) { | |
if(fn.canRun) { | |
let _this = this; | |
fn.canRun = false; | |
fn.call(_this, ...args); | |
setTimeout(() => { | |
fn.canRun = true; | |
}, delay); | |
} | |
} | |
} | |
var throttleConsole = throttle((val) => { | |
console.log(`${val}:` + new Date()); | |
}, 1000); | |
let i=0; | |
document.getElementById('btn').addEventListener('click', () => { | |
throttleConsole(i++); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment