0

how do I call a function once after all cookies.onChange events fire, I kown

chrome.cookies.onChange.addListener  

but it call event handler everytime after every cookies get changed, I want to call event handler only once after all events fired.

How I can do that?

Thank you!

4

1 回答 1

0
    var hasBeenCalled = false;
    function doSomethingOnce() {
       if (!called) {
          console.log("I haven't been called before");
          // do fancy stuff.
          hasBeenCalled = true;
       } else {
          // do nothing, function already called.

       };
    };

chrome.cookies.onChange.addListener(doSomethingOnce);

或者更好的是,您可能会找到一个chrome.cookies.onChange.removeListener(doSomethingOnce);,在这种情况下,在您的doSomethingOnce函数中,在您执行任何您想做的事情之后,您调用chrome.cookies.onChange.removeListener(doSomethingOnce);

于 2013-04-29T13:52:13.077 回答