Jump to content

ClearTimeout doesnt appear to be responding


612wharfavenue

Recommended Posts

My script is a simple onclick instance, so the first one says "if you click on me set a timeout to execute the 2nd script" which works fine, and the 2nd script says " if you click on this other thing, do this stuff and cancel the timeout while your at it". The problem is that it doesnt cancel the timeout. Ive tried using "var t = 0;" and "var t;" in either and both scripts, as well as its own script. Seems to be like the clearTimeout isnt reading the variable correctly.

<script>var t = 0;$(function() {	 $('.clickme').click(function(){	var t = setTimeout("dothisthing()", 1000);functionstuff.....});});</script><script>$(function() {	$('.dothisthing').click(dothisthing);});function dothisthing() {clearTimeout(t);functionstuff...};</script>

Link to comment
Share on other sites

You have declared two variables named t. One is in the global scope, and one is local to your click event handler. That's the variable your timer is assigned to. The dothisthing function is operating on the global variable. The easiest solution is to remove the local variable so that t always identifies a global variable.

Link to comment
Share on other sites

Im googling the ###### out of this one and ive only seen one problem similar to mine and the op never said what he did to fix it. I'm doing everything by the book, is there something wrong with this because its not working:

<script>var t = 0;$(function() {	 $('.clickme').click(function(){	setTimeout("dothisthing()", 1000);functionstuff.....});});</script><script>$(function() {	$('.dothisthing').click(dothisthing);});function dothisthing() {clearTimeout(t);functionstuff...};</script>

Link to comment
Share on other sites

i got rid of the local one and its still malfunctioning. How would setTimeout know where to store its value if i didnt specify "var t = setTimeout()"?
don't use the var keyword in front of it. once you declare it as var t in the global scope, all you have to do from there on in is use just t = xxx. Using the var keyword within another scope will scope that variable to that scope.edit: aside from expressing your frustration, have you checked to see if there are errors? Have you added alert statements or logs to the console to to verify that your event handlers are actually working, and that they trigger the function you want? It's best to test the steps one at a time, tracing events as they happen to see where the bug is occurring.
Link to comment
Share on other sites

To clarify thescientist's explanation with some code. Your code now looks like this:

var t = 0; // THIS WORKS$(function() {   $('.clickme').click(function(){   setTimeout("dothisthing()", 1000); // THIS DOESN'T

The last line was over-corrected. Change the sequence to look like this, which is very close to your original:

var t = 0;$(function() {   $('.clickme').click(function(){   t = setTimeout("dothisthing()", 1000); // ASSIGN THE VALUE TO *GLOBAL* t

Link to comment
Share on other sites

Oh, that did it. thanks. I thought global var took priority over local, or would at least share it with local, guess not.
it will "share" it, but not if you re-declare it with the var keyword in the local scope.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...