Skip to content

Commit 34f0217

Browse files
committed
throttle&debounce
1 parent 2bbdc6e commit 34f0217

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const btn=document.querySelector("#btn");
2+
const bc=document.querySelector("#bc");
3+
const tc=document.querySelector("#tc");
4+
5+
var c_count=0, t_count=0;
6+
7+
const mydebounce=(cb,d)=>{
8+
let timer;
9+
return(...args)=>{
10+
if(timer) clearTimeout(timer);
11+
timer=setTimeout(()=>{
12+
cb(...args);
13+
},d);
14+
}
15+
}
16+
17+
const debounce_apply=mydebounce((...args)=>{
18+
tc.innerHTML=++t_count;
19+
console.log(args[0]+args[4]);
20+
},1000);
21+
22+
btn.addEventListener("click",()=>{
23+
bc.innerHTML=++c_count;
24+
debounce_apply(5,6,7,8,9);
25+
})

AdvancedJS/Throttle&Debounce/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ <h1>THROTTLING & DEBOUNCING</h1>
1010
<button id="btn">CLICK ME</button>
1111
<p>CLICKED: <span id="bc"></span></p>
1212
<p>TRIGGERED: <span id="tc"></span></p>
13-
<!-- <script src="debounce.js"></script> -->
14-
<script src="throttle.js"></script>
13+
14+
<!-- <script src="debounce.js"></script> -->
15+
<!-- <script src="throttle.js"></script> -->
1516
</body>
1617
</html>

0 commit comments

Comments
 (0)