Results 1 to 8 of 8

Thread: SetTimeout help

  1. #1
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy SetTimeout help

    Why doesn't setTimeout() work here? I check the error console and it repeats over and over disregarding the 2000 millisecond timeout.
    Code:
    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.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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?
    Jeremy | jfein.net

  3. #3
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Ok, but the code works perfectly for me, is there anything specific that doesn't work? What browser are you using?
    Jeremy | jfein.net

  5. #5
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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

    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

    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

    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

    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.

  7. #7
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You're a genius

    Sounds like the fix, but it is all doing the same thing.
    Last edited by itsjareds; 07-01-2008 at 06:25 PM.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

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

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •