The SetTimeout Function

From :
http://www.pageresource.com/jscript/jtimeout.htm

<HEAD>
<SCRIPT language="JavaScript">
<!--hide
function newtext()
{
  document.myform.mytext.value
="Hey! I have changed!";
  setTimeout(
"moretext()",1000);
 }

function moretext()
{
  document.myform.mytext.value
="I just change with the time!";
}


//-->
</SCRIPT>
</HEAD>

<BODY>
<FORM name="myform">
<INPUT type="text" name="mytext" value="Not Much Here." size="30">
&nbsp;&nbsp;
<INPUT TYPE="button" name="but1" value="Click!" onClick="newtext()">
</FORM>


We use the HEAD section to define our two functions. The first function is named "newtext()". We call this function when the user clicks the button we created. As you can see, we changed the value of the text in the text box by using the name of the form and the name of the textbox:

document.myform.mytext.value="Hey! I have changed!";

The next command is what makes the second change happen. We call the function "moretext()" after 1000 milliseconds:

setTimeout("moretext()",1000);

Now, the function "moretext()" changes the value in the form box one more time, after waiting 1 second:

document.myform.mytext.value="I just change with the time!";

Now, in the BODY section, we simply put the code for the form and the textbox. Remember to give your form and your textbox names. That is how we are able to change the text in the box using the functions. The name for the form above was "myform", and the name of the text box was "mytext".

posted @ 2007-05-16 10:51  jhtchina  阅读(293)  评论(0编辑  收藏  举报