I'm trying to remove a script added by setTimeout(). I tried to use clearTimeout() but seems like it's not working.
This is what should happen: Click Btn1 to include the function for btn2. After btn1 is clicked, click Btn2. An alert will be displayed. When btn1 is clicked again, the previous function added by settimeout should be cleared. And another function for btn2 will be added. Click on btn2, only one alert should be displayed.

But what happens on my end is when I click btn1 two times, the alert for btn2 is displayed twice too. Shouldn't the previous function for btn2 be remove since I used clearTimeout in btn1 too?

Here' the jsfiddle for my problem: http://jsfiddle.net/goo2gvmj/

Any help will be greatly appreciated.


4 Years
Discussion Span
Last Post by mattster


I've tried to replicate what you want, but instead using a little function instead of setTimeout(). setTimeout() isn't really meant for this sort of thing, whereas my little function will produce hopefully the same result, but maybe slightly better practice.

Take a look at the example to see what you think: jsFiddle.


It is not at all clear why you got timeouts involved here. clearTimeout in your code would discard the previously assigned timeout but do nothing whatsoever to disconnect #btn2 from the click handler that was assigned to it. If no then go and click #btn1 you are assigning a whole new click event handler to #btn2 which now has two of em - the fact that the two handlers are one and the same function is of no relevance. The end result - you get your multiple alerts:one for each time you clicked #btn1.

If you do not want to have the event on a target element being handled multiple times you should first discard the previously assigned handler.

Take a look at this fiddle. Also, read the relevant jQuery documentation.



I'm trying see if I can remove scripts added in a window by a setTimeout function. I'm using clearTimeout thinking it will remove the previous scripts added by setTimeout. I need to remove the previous scripts so just like in the example, btn2 will not alert twice when I click btn1 two times.



Sorry but I need to get my head around this:

@jresponse - why have you created another fiddle, that is a million times more complicated to handle? Using bind methods here is very confusing, so god knows how a newbie will understand (no offence @cmsc). The fact that no one can understand it or what it is trying to achieve is not helpful. At all.

I do completely and utterly agree with this however:

It is not at all clear why you got timeouts involved here.

@cmsc - is this homework or something? Because I'm struggling to know why you're using timeouts. There really is no need to bother with timeouts with this sort of thing, as it's not what the function is meant to be used as. Really it's meant to time functions to be executed, say 5 seconds after page load call function loadMessage().

At least both me and @jresponse are both agreed that the solution to your problem lies in javascript functions, and definately not timing events.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.