Basically:
You click a button and it runs function 1.
If you don't click the button again within 1 second then function 2 runs.
If you click the button within 1 second then it runs function 1 again.
and so on and so forth...
I can't figure out the logic to do this in Javascript.
Is there even a way?
Thanks in advanced!
Using
setTimeout
andclearTimeout
:from the top of my head (haven't tested this, but this seems most logic to me):
This should do it:
See http://jsfiddle.net/alnitak/QZRTA/
The outer function block serves to keep the
timer
variable in the local scope without creating a global variable.http://jsfiddle.net/dfsq/rrXWt/
You'll want to use a timer to keep track of the time. You can use
setTimeout
to run function 2 in 1 second (1000ms). If, however, you click button again, you should stop the timer. You can do that usingclearTimeout
.The core lines would be: