1.html
<div id="app" style="width:400px;height:400px;border:1px solid red;"></div> <h1 id="h1"></h1>
2.js
// 鼠标在app元素里面移动 鼠标停止后一秒钟在执行函数 防抖
let app = document.getElementById('app')
let h1 = document.getElementById('h1')
let timer = null
let num = 0
app.onmousemove = function () {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
num ++ // 等待鼠标 停止移动1秒钟后在执行
h1.innerHTML = '执行的次数: ' + num
}, 1000)
}
3.应用 :搜索联想词功能类似