| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>快捷报表</title>
- </head>
- <body>
- <div>建设中....</div>
- <button onclick="debounceTask">testDebounce</button>
- </body>
- <script>
- //防抖
- function debounce (fn , delay) {
- let timer;
- return function (...args) {
- if (timer) {
- clearTimeout(timer)
- }
- timer = setTimeout(()=>{
- fn.apply(this,args)
- },delay)
- }
- }
- //test
- function task() {
- console.log('run')
- }
- const debounceTask = debounce(task,1000);
- //节流
- function throttle(fn , delay) {
- let last = 0; //last time
- return function (...args) {
- const now = Date.now();
- if(now - last > delay){
- last = now;
- fn.apply(this,args)
- }
- }
- }
- function task2() {
- console.log('run2')
- }
- const throttleTask = throttle(task2,1000)
- </script>
- </html>
|