JavaScript Counter Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HawkNail
    New Member
    • May 2007
    • 5

    JavaScript Counter Error

    Long story short, I'm making an online test to help prepare some buddies for an online math test. The test is timed and so I am trying to put in a counter to count down to zero. My code looks like this:

    Code:
    function clock()
    {
    	if (document.clock.display.value>0){
    	setTimeout("clock()",60);
    	document.clock.display.value = document.clock.display.value - 1;
    	document.clock.display.write("document.clock.display.value");
    	document.clock.display.close();
    	}
    	else {
    	alert("Your time is up!");
    	}
    }
    It is started by an onLoad from the body tag. It links to:

    Code:
    <form name="clock">
    <input type="textarea" name="display" value="30">
    </form>
    Now the thing is - it works, and it gets to zero and throws up the alert. However, IE is bent on telling me that the "object doesn't support that property or method". What's going on here? Sure it works, but I'm kind of a perfectionist and bad code is not okay.
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Why you have used this line.

    Code:
     document.clock.display.write("document.clock.display.value");
    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • HawkNail
      New Member
      • May 2007
      • 5

      #3
      I used that line to rewrite the value of the "clock" so it is visible to the user - but I just tried it w/o that line and it worked. So thanks for the answer, but would you care to explain how it works now?

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        using document.write( ) after a page is fully rendered destroys the DOM and so it will not work anymore ... it is not suggested to use that after a page is loaded ... you could always use other methods ...

        kind regards

        Comment

        • RamananKalirajan
          Contributor
          • Mar 2008
          • 608

          #5
          Well Said Gits.

          @HawkNail,

          Code:
          document.clock.display.value = document.clock.display.value - 1;
          This line will do your desired work. Again using document.write for the same object will leads to error. You can use document.write before loading the page or on the load of the page. To edit the contents after the page is loaded use DOM functionality as "Gits" said


          Thanks and Regards
          Ramanan Kalirajan

          Comment

          Working...