SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trouble with SetTimeOut

    After searching this forum, I'm still having some problems. Basically, I have a form on a page that I want to be timed. After a certain period of time, the form submits itself.
    Code:
    <script type="text/javascript" language="JavaScript">
    function beginCountDown(val)	{
    	settimeout("beginCountDown()", val * 1000)
    	document.form.submit()	
    	}
    </script>
    
    <body onload="beginCountDown(5)">
    I keep getting a "Error: Object Expected"
    Thanks
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  2. #2
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    There are a few things wrong with your code that I see. First of all, I'm not sure if it's case-sensative, but I have a felling it might be, so use:

    Code:
    setTimeout
    Secondly, object expected error means something's not there. Make sure you have a form named "form." And that in yoru form, you don't have a button named "submit." It's always a bad idea to name any button "submit." This is because JavaScript doesn't know whether you want to access the element or the submit() method of the form object. It tries to do the former, usually producing an error.

    Next, you are trying to use a recurssive function. But, the design is flawed:

    Code:
    function beginCountDown(val)	{
    	settimeout("beginCountDown()", val * 1000)
    	document.form.submit()	
    	}
    When the code is run, it tells it to run that same function again (in 5 seconds as specified by your argument), but it also tells the form to submit (even when it's run for just the first time). To overcome this, I'd use:

    Code:
    function beginCountDown(val)	{
    	setTimeout("document.form.submit()", val * 1000)
    }
    Code:
    <body onload="beginCountDown(5)">
    And hopefully that should work.

    aDog

  3. #3
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help. I have a small reference book on JavaScript and it explained the setTimeout being used to call functions only and all the examples were set-up in a recursive way (scrolling marquees and such).

    Works great now. Although, I may change it to recursive or place it in a loop so that a cout down timer is displayed on the page, i.e. 5 - 4 - 3 - 2 ...
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  4. #4
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess I didn't know as much JavaScript as I thought. I got the timing itself to work, but not the counter. The following code causes it to simply keep reloading
    Code:
    <script type="text/javascript" language="JavaScript">
    function beginCountDown(val)	{
    	for(i=val;i>0;i--)	{
    		setTimeout("document.form.ele1.value=" + i, 1000)
    	}
    	document.form.submit();
    }
    </script>
    Although there are no errors.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  5. #5
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Let's think about how this works:

    function beginCountDown(val) {
    for(i=val;i>0;i--) {
    setTimeout("document.form.ele1.value=" + i, 1000)
    }
    document.form.submit();
    }


    The for() loops runs as fast as possible. Let's say it takes 1 microsecond for JavaScript to run the loop. Then, what you're saying is:

    setTimeout("document.form.ele1.value="+i,1000);
    setTimeout("document.form.ele1.value="+i,1001);
    setTimeout("document.form.ele1.value="+i,1002);
    setTimeout("document.form.ele1.value="+i,1003);
    setTimeout("document.form.ele1.value="+i,1004);


    Instead, here's what I would use:

    Code:
    function beginCountDown(val){
         if(val==0){
              document.form.submit();
         }else{
              document.form.ele1.value=val;
              setTimeout("beginCountDown("+(val-1)+")",1000);
         }
    }
    aDog

  6. #6
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are one smart SOB, Arielladog!
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  7. #7
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by westmich
    You are one smart SOB, Arielladog!
    Thanks...I think

    aDog


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
  •