0

My question is referring onto this question.

At the moment I am using this answer, but I still can trick that code, so the clearInterval won't stop. While holding my left mouse button I simple press the right one to open the contextmenu. Now the clearInterval wasn't called.

If I add the contextmenu event to the clicker, with a clearInterval it does get called, but it won't clear the interval.

See this demo

Community
  • 1
  • 1
Steckdoserich
  • 834
  • 2
  • 11
  • 24

1 Answers1

2

Add clearInterval() to the mousedown event:

clicker.mousedown(function() {
  clearInterval(timeout);
  timeout = setInterval(function() {
    clicker.text(count++);
  }, 500);

  return false;
});

That will be triggered on the context menu, and it will prevent multiple timers from running at once.

Fiddle

Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
  • Crap, that's a distinctly better solution than popped into my head. But I'll leave the important bit of my answer here: Setting a `setInterval` to a var only saves an identifying record, not the interval itself. – DBS Mar 30 '16 at 18:36
  • I thought your answer was fine and was surprised to see it deleted. Good point re "Setting a `setInterval` ..." – Rick Hitchcock Mar 30 '16 at 18:38
  • 1
    I already tried to write a comment @DBS answer, because I was still able to "cheat" with fast clicks. I think this was caused through the fact of multiple access on one variable. At the moment I'm not able to "cheat" on yours (@RickHitchcock) ;) – Steckdoserich Mar 30 '16 at 18:41