Skip to content

Instantly share code, notes, and snippets.

@reygreen1
Created May 27, 2020 04:21
Show Gist options
  • Save reygreen1/009f0aec0c87f6256d6776a2f589fa83 to your computer and use it in GitHub Desktop.
Save reygreen1/009f0aec0c87f6256d6776a2f589fa83 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/pupudim
<!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>
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