Preventing a JavaScript event listener from firing multiple times concurrently (in a Google Chrome extension) -
i've set google chrome extension creates context-menu of user's bookmarks. contextmenus api must implemented through background page, added following event listeners update context-menu if there changes in user's bookmarks:
chrome.bookmarks.onchildrenreordered.addlistener(function () { chrome.contextmenus.removeall(); contextmenu() }); chrome.bookmarks.onmoved.addlistener(function () { chrome.contextmenus.removeall(); contextmenu() }); chrome.bookmarks.oncreated.addlistener(function () { chrome.contextmenus.removeall(); contextmenu() }); chrome.bookmarks.onremoved.addlistener(function () { chrome.contextmenus.removeall(); contextmenu() }); chrome.bookmarks.onimportended.addlistener(function () { chrome.contextmenus.removeall(); contextmenu() });
it works, part, i've come across 1 bug cannot work out how kill. namely, if change multiple bookmarks concurrently (for example, selecting multiple items in bookmarks manager , re-arranging them), script fires multiple times concurrently, , end multiple instances of context-menu.
can offer suggestions on how resolve this?
i've never worked chrome extensions before, if ever want prevent things happening side-by-side can this:
- have variable called "lasteventtime"
- every time event happens, update variable current timestamp
- every time event occurs, check see if current time 2 seconds after stored time. if not, "return;".
this should make sure events happen less 2 seconds after first event ignored.
i'm not sure if solves problem, hope points in right direction.
Comments
Post a Comment