javascript - How to stop calling a delayed function if some other event happens in that period? -
after event happens, have function (rightmenu) triggers 1.5s delay. however, challenge figure out how check if leftmenu called during period stop settimeout function call rightmenu.
var leftmenu = function() { // code here } var rightmenu = function() { // code here } $('#leftmenu').on('click', function() { leftmenu(); }) $('#rightmenu').on('click', function() { rightmenu(); }) if (...) { leftmenu(); } else { settimeout(function() { rightmenu(); }, 1500); }
i'm not 100% sure if you're asking, can clear intervals. example:
function asyncfun() { // code } settimeout(asyncfun, 5000); // run after 5 seconds var asynchandle = settimeout(asyncfun, 10000); cleartimeout(asynchandle); // cancels function call
i feel you're asking...
if not, other interpretation have want temporarily remove event handler #leftmenu
, #rightmenu
when you're in other menu. this, can clear event handlers in jquery $("#rightmenu").off("click")
. function opposite of .on
. see here. luck!
yet possible fix code:
/* initialize variable here. technically doesn't change because of hoisting, i'm guessing based on question haven't learned yet. */ var callingright; var leftmenu = function() { // code here } var rightmenu = function() { // code here } $('#leftmenu').on('click', function() { cleartimeout(callingright); // clear timeout on global variable here. leftmenu(); }) $('#rightmenu').on('click', function() { rightmenu(); }) if (...) { leftmenu(); } else { callingright = settimeout(function() { // assign settimeout global variable rightmenu(); }, 1500); }
Comments
Post a Comment