diff --git "a/\346\265\217\350\247\210\345\231\250\346\270\262\346\237\223/async\344\270\216defer.png" "b/\346\265\217\350\247\210\345\231\250\346\270\262\346\237\223/async\344\270\216defer.png" new file mode 100644 index 0000000..1ad3b3c Binary files /dev/null and "b/\346\265\217\350\247\210\345\231\250\346\270\262\346\237\223/async\344\270\216defer.png" differ diff --git "a/\346\265\217\350\247\210\345\231\250\346\270\262\346\237\223/index.html" "b/\346\265\217\350\247\210\345\231\250\346\270\262\346\237\223/index.html" new file mode 100644 index 0000000..79d0140 --- /dev/null +++ "b/\346\265\217\350\247\210\345\231\250\346\270\262\346\237\223/index.html" @@ -0,0 +1,23 @@ + + + + + + Document + + + +
+
+ + \ No newline at end of file diff --git "a/\351\230\262\346\212\226\345\222\214\350\212\202\346\265\201/debounce.js" "b/\351\230\262\346\212\226\345\222\214\350\212\202\346\265\201/debounce.js" new file mode 100644 index 0000000..d233a3b --- /dev/null +++ "b/\351\230\262\346\212\226\345\222\214\350\212\202\346\265\201/debounce.js" @@ -0,0 +1,20 @@ +const input = document.getElementById('input') + +input.addEventListener('keyup', debounce(ajax, 300)) + + +function ajax(e) { + console.log(e.target.value) + console.log(this) + console.log('ajax 请求') +} + +function debounce(fn,delay) { + let timer + return function(...args) { + clearTimeout(timer) + timer = setTimeout(() => { + fn.apply(this, args) + }, delay) + } +} \ No newline at end of file diff --git "a/\351\230\262\346\212\226\345\222\214\350\212\202\346\265\201/index.html" "b/\351\230\262\346\212\226\345\222\214\350\212\202\346\265\201/index.html" new file mode 100644 index 0000000..08e823b --- /dev/null +++ "b/\351\230\262\346\212\226\345\222\214\350\212\202\346\265\201/index.html" @@ -0,0 +1,15 @@ + + + + + + Document + + + 函数防抖: + 函数节流: + + + + + \ No newline at end of file diff --git "a/\351\230\262\346\212\226\345\222\214\350\212\202\346\265\201/throttle.js" "b/\351\230\262\346\212\226\345\222\214\350\212\202\346\265\201/throttle.js" new file mode 100644 index 0000000..2c5f936 --- /dev/null +++ "b/\351\230\262\346\212\226\345\222\214\350\212\202\346\265\201/throttle.js" @@ -0,0 +1,31 @@ +const input2 = document.getElementById('input2') + +input2.addEventListener('keyup', debounce(ajax2, 1000)) + +function ajax2(e) { + console.log(e.target.value) + console.log(this) + console.log('ajax 请求') +} + +function throttle(fn, delay) { + let timer + return function(...args) { + if(timer) return + timer = setTimeout(() => { + fn.apply(this, args) + timer = null + }, delay) + } +} + +function throttle2(fn, delay) { + let pre = 0 + return function (...args) { + let now = new Date() + if(now - pre > delay) { + fn.apply(this, ...args) + pre = now + } + } +} \ No newline at end of file