console
// function onButton() {
// console.log('click')
// }
let onButton = throttle(function() {
console.log('click')
}, 1000)
let onButton1 = debounce(function() {
console.log('click')
}, 1000)
//节流, 点击后,没100秒必执行一次
function throttle(fn, delay=100) {
let timer
return function() {
if(timer) {
return
}
timer = setTimeout(()=> {
fn.apply(this, arguments)
timer = null
}, delay)
}
}
//防抖
function debounce(fn, delay = 1000) {
let timer = null
return function() {
if(timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this, arguments)
timer = null
}, delay)
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=, initial-scale=">
<meta http-equiv="X-UA-Compatible" content="">
<title></title>
</head>
<body>
<button onclick="onButton()">节流点击</button>
<button onclick="onButton1()">防抖点击</button>
<script>
// function onButton() {
// console.log('click')
// }
</script>
</body>
</html>