我用下划线创建一个去反跳的函数版本:

var debouncedThing = _.debounce(thing, 1000);

一旦去抖动,就称为...
debouncedThing();

...在它真正执行之前的等待期间,有什么办法可以取消它?

最佳答案

如果您使用lodash的最新版本,则可以执行以下操作:

// create debounce
const debouncedThing = _.debounce(thing, 1000);

// execute debounce, it will wait one second before executing thing
debouncedThing();

// will cancel the execution of thing if executed before 1 second
debouncedThing.cancel()

另一个解决方案是使用标志:
// create the flag
let executeThing = true;

const thing = () => {
   // use flag to allow execution cancelling
   if (!executeThing) return false;
   ...
};

// create debounce
const debouncedThing = _.debounce(thing, 1000);

// execute debounce, it will wait one second before executing thing
debouncedThing();

// it will prevent to execute thing content
executeThing = false;

关于javascript - 如何在调用后且执行之前取消反跳功能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29012922/

10-13 02:34