是否有一种很好的方法可以为_.debounce
获得类似的行为,但区别在于每个更改的参数都执行一次?例如:
foo = function (arg) {console.log(arg)}
lazy = _.debounce(foo,1000)
lazy("a")
lazy("a")
lazy("a")
lazy("a")
lazy("b")
a // with debounce "a" would not be printed
b // but i would like run the debounced function once for every changed arg.
答案 0 :(得分:1)
由于缺乏更好的解决方案,我想出了这个:
args = {}
function markAllAsUnread(rooms) {
_.each(rooms,function (roomname) {
// Here i do my heavy work....
console.log(roomname)
})
args = {}
}
markAllAsUnreadLazy = _.debounce(markAllAsUnread,2000)
function markAsUnread (arg) {
args[arg] = arg // to unify the given args
markAllAsUnreadLazy(args)
}
markAsUnread (1)
markAsUnread (1)
markAsUnread (123)
markAsUnread (123123)
markAsUnread (123123)
markAsUnread (123123)
markAsUnread (12334)
1
123
12334
123123