PDA

View Full Version : SetTimeout help



itsjareds
06-30-2008, 11:54 PM
Why doesn't setTimeout() work here? I check the error console and it repeats over and over disregarding the 2000 millisecond timeout.

function remove(a) {
window.location.href = "http://www.spogg.com/members/guestbook.php?member=KnifeySpooney&remove_id=" + a;
}

for (var a=496; a>0; a--) {
setTimeout("remove(a)", 2000);
}


Also, it says in the error console: remove is not defined.

Nile
07-01-2008, 01:15 AM
This script works perfectly, but I don't see why your redirecting so many times, because in the end it's gonna end up at 0.
Did you want it to open a new window 496 times?

itsjareds
07-01-2008, 02:07 AM
Yes, I'm trying to delete a lot of entries from my guestbook on a site (about 400)

I want to give it some time to load each time, like 2 seconds to load the site. Then it will reload and delete the next one.

However, it's going into a loop that goes as fast as it can.

Nile
07-01-2008, 03:36 AM
Ok, but the code works perfectly for me, is there anything specific that doesn't work? What browser are you using?

itsjareds
07-01-2008, 06:06 AM
Specifically, everything doesn't work. My computer is adding errors as fast as it can to the error console saying "remove is not defined".

So every second about 5 errors come saying "remove is not defined."

I am using Firefox 3.

codeexploiter
07-01-2008, 07:10 AM
I've tested the problem in Firefox 2.0.0.14 using Firebug 1.05 and also in Firefox 3.0 using Firebug 1.2.0b4 and found the following results:

FF 2.0.0.14-Firebug 1.05

Tested the following code



for (var a=496; a>0; a--) {
setTimeout("remove(a)", 2000);
}


It shown an error - "a is not defined" from starting to the loop to the end.

FF 3.0-Firebug 1.2.0b4

Tested the following code



for (var a=496; a>0; a--) {
setTimeout("remove(a)", 2000);
}


When I tried to log the value of a from the remove function it simply shown it as "0" in all the iteration of the loop.



I've chanaged the code a bit and tested it:

FF 2.0.0.14-Firebug 1.05

Tested the following code



for (var a=496; a>0; a--) {
setTimeout("remove("+ a +")", 2000);
}


When I tried to log the value of "a" from the remove function, it correctly displayed the value of "a" that.

FF 3.0-Firebug 1.2.0b4

Tested the following code



for (var a=496; a>0; a--) {
setTimeout("remove("+ a +")", 2000);
}


When I tried to log the value of "a" from the remove function, it correctly displayed the value of "a" that.

Hope this helps.

itsjareds
07-01-2008, 06:15 PM
You're a genius :D

Sounds like the fix, but it is all doing the same thing.

jscheuer1
07-01-2008, 06:51 PM
Setting so many timeouts may be the problem, since they are all set virtually at once, if they all execute on time, it will look like all the 'a's got removed at once, about 2 seconds after the loop ran. If you want to remove decremental 'a's at 2 second intervals, something like this would be good:


var removingA = function (a){
if(!a)
return;
remove(a--);
setTimeout(function(){removingA (a);}, 2000);
}
removingA (496);