- //es6语法
export function debounce(func, delay) { - let timer
- //返回一个函数,并拿到参数
- return function (...args) {
- if (timer) {
- clearTimeout(timer)
- }
- timer = setTimeout(() => {
- func.apply(this, args)
- }, delay)
- }
- }
- //简单实现
var debounce = function(idle, action){ - var last
- return function(){
- var ctx = this, args = arguments
- clearTimeout(last)
- last = setTimeout(function(){
- action.apply(ctx, args)
- }, idle)
- }
- }