原文地址: https://www.jeremyjone.com/704/,转载请注明。
什么是防抖
事件响应函数在一段时间后才执行,如果在这段时间内再次调用,则重新计算执行时间;当预定的时间内没有再次调用该函数,则执行该函数。
防抖做什么
防止某些函数的频繁调用,保证页面的稳定流畅和数据准确性。
一个小的例子
使用 underscore 的防抖功能来测试一下效果。
在页面中直接导入cdn即可。
https://cdn.bootcss.com/underscore.js/1.9.1/underscore.js
未防抖时的样子
将下面内容粘贴到一个HTML的body标签中。
<div id="container" style="width:100%;height:200px;line-height:200px;text-align:center;color:#fff;background-color:#444;font-size:30px;"></div>
<script>