Created
April 8, 2020 09:55
-
-
Save watab0shi/46a44ba659d4effde12df594f1af57a6 to your computer and use it in GitHub Desktop.
ES6 throttle debounce
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
<style> | |
body { height: 500vh; background: linear-gradient(to top, #051937, #004d7a, #008793, #00bf72, #a8eb12); } | |
#debug { position: fixed; color: white; font-family: 'Menlo', monospace; font-size: 24px; bottom: 0; } | |
</style> | |
</head> | |
<body> | |
<div id="debug"> | |
<p>scrollY : <span id="txt-scrollY">0</span></p> | |
<p>window.innerWidth : <span id="txt-width">0</span></p> | |
</div> | |
<script src="index.js"></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
/** | |
* cite: | |
* Throttling and debouncing in JavaScript - codeburst | |
* https://codeburst.io/throttling-and-debouncing-in-javascript-646d076d0a44 | |
*/ | |
/** | |
* 一定間隔で処理を間引くやつ | |
* @param {Number} interval | |
* @param {Function} fn | |
*/ | |
const throttle = (interval, fn) => { | |
let lastTime = performance.now() - interval; | |
return (...args) => { | |
const time = performance.now(); | |
if (time - lastTime < interval) return; | |
lastTime = time; | |
fn(...args); | |
} | |
}; | |
/** | |
* 連続して発火するイベントの最後だけ発火させるやつ | |
* @param {Number} delay | |
* @param {Function} fn | |
*/ | |
const debounce = (delay, fn) => { | |
let timerId = null; | |
return (...args) => { | |
if (timerId) clearTimeout(timerId); | |
timerId = setTimeout(() => { | |
fn(...args); | |
timerId = null; | |
}, delay); | |
}; | |
}; | |
class Page { | |
constructor() { | |
this.scrollYTextElement = document.getElementById('txt-scrollY'); | |
this.scrollYTextElement.textContent = window.scrollY; | |
this.widthTextElement = document.getElementById('txt-width'); | |
this.widthTextElement.textContent = window.innerWidth; | |
window.addEventListener('scroll', throttle(100, (e) => { this.onScroll(e); })); | |
window.addEventListener('resize', debounce(100, (e) => { this.onResize(e); })); | |
} | |
onScroll(e) { | |
// console.log(e.type, window.scrollY); | |
this.scrollYTextElement.textContent = window.scrollY; | |
} | |
onResize(e) { | |
// console.log(e.type, window.innerWidth); | |
this.widthTextElement.textContent = window.innerWidth; | |
} | |
} | |
document.addEventListener('DOMContentLoaded', () => { | |
const page = new Page(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment