debounce trottle

/***

* @param fn {Function} 实际要执行的函数

* @param delay {Number} 延迟时间,也就是阈值,单位是毫秒(ms)

* * @return {Function} 返回一个“去弹跳”了的函数 */

function debounce(fn, delay) {var timer return function () { var that = this var arg = arguments clearTimeout(timer) timer = setTimeout(function () { fn.apply(that, arg) } , delay) }}var div = document.getElementsByTagName('div')[0];div.style.width = '100px'div.style.height = '100px'div.style.background = "#f00"div.onmouseover = debounce(function () { console.log(111) }, 300)


/** * * @param fn {Function} 实际要执行的函数 * @param delay {Number} 执行间隔,单位是毫秒(ms) * * @return {Function} 返回一个“节流”函数 */

function debounce(fn, threshhold) { var timer var last threshhold || (threshhold = 250) return function () { var now = +new Date() var that = this var arg = arguments if (last && now < last + threshhold) { clearTimeout(timer) timer = setTimeout(function () { fn.apply(that, arg) } , threshhold) } else { fn.apply(that, arg) } last = now }}var div = document.getElementsByTagName('div')[0];div.style.width = '100px'div.style.height = '100px'div.style.background = "#f00"div.onmouseover = debounce(function () { console.log(111) }, 1000)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容