JS settimeout Usage Detailed

Source: Internet
Author: User
Tags modulus setinterval

<body><div id= "Test" >test block</div></body>

Window.settimeout (Func,delay);

Func that represents the function or code string to be executed.

Delay, delay parameter, in milliseconds, one second =1000 milliseconds.

When a function or statement passed in Func has a variable input, the value of the variable is taken as the value of the execution period after the delay, and the value of the code execution period is not the same. For example:

Code:

for (Var i=1;i<=50;i++) {window.settimeout ("document.write (i)", 1000);}

The meaning of this writing is to print 50 I after the program has finished 1 seconds, at which time the program has been executed and the value of I is "51";

If the program prints a number every 1 seconds, the program is rewritten as

Code:

for (Var i=1;i<=50;i++) {window.settimeout ("document.write (" +i+) ", i*1000);}

With "+i+" the string jumps out, each execution of the assignment

1. Window.settimeout ("document.write (1)", 1000);

2. Window.settimeout ("document.write (2)", 2000);

3. Window.settimeout ("document.write (3)", 3000);

4. Window.settimeout ("document.write (4)", 4000);

5. Window.settimeout ("document.write (5)", 5000);

...

...

Window.settimeout ("document.write (50)", 50000);

This enables the display of the delay

--------------------------------------------------------------------------------

SetInterval () This function is the same as setttimeout, but also delay execution, but with settimeout () is not the same place, settimeout execution stopped, and SetInterval will continue to carry out ...


<style>
div {position:relative;
}
</style>

<title>animating text</title>
<script language= "JavaScript" type = "Text/javascript" >
var pos1 = 0;

function f () {
POS1 + 5;
if (Pos1 > 640)
POS1 = 0;
document.getElementById ("MoveText"). Style.left = POS1;
Window.settimeout ("f ();", 300);
}
</SCRIPT>
<body onload= "f ();" >
<div id= "MoveText" >
<b>inside a div</b>
</div>
</body>


1. settimeout ()

1.1 settimeout () syntax example

1.2 Perform function with settimeout ()

1.3 settimeout () syntax example

1.4 Set the condition to stop settimeout ()

1.5 Score and seconds of counter

2. Cleartimeout ()

3. Set Flag


 


--------------------------------------------------------------------------------

10.1 settimeout ()
SetTimeout () is the method of Fu window, but I?? is to omit window??? Zhang?? Q, is it a??? with a DUI O? R?,? r? The sash is lucky?? The specified method.? First look at the following????? ? Yes?] Yes??? The use of examples, just use? xing unexpectedly? SetTimeout () of the law.

 

1. settimeout () Law Example
 

?? -69 wait three seconds. Sash alert ()

In the 3rd chapter, to alert Gamma box, it is usually used to call out? In???, you?? Blowing Arrogant W??? ⑨ 3 seconds, just??? Ying 霈 f one?? Alert Gamma box.

1. What's the use of the G-device? ⑹ the timeout1.htm in the disk?? The N case has the following heat?

<p> wait three seconds!

<script>
SetTimeout ("The" alert (') Σ yellow? Want you to Wait ")", 3000)
</script>

</body>

2. Pay attention to???? ⑨?? Just do it. 霈 f one?? Alert Gamma box.

 

SetTimeout () is a. Designate wait for R? (х millisecond), "R", "G"???? The sash is lucky?? The specified method or function has the following methods:

This example is set to wait for 3 seconds (3000 milliseconds), the G. The sash alert ()? Method

 

2. With settimeout ()? Sash function
SetTimeout () is usually? function together, the following is a??? On?????? The example.

?? -70?? In column B from у?/p>

In?? -20, you see? How to use a press? b column text, and then use the press to eliminate text, in the???, you see how to use the press? Column B shows the text, but the words?? Anthony 脶 Clenbuterol?а?/p>

1. What's the use of the G-device? ⑹ the timeout2.htm in the disk?? The N case has the following heat?

<script>
function Clearword ()
{window.status= ""}
</script>

<form>
<input type= "button" value= "in?" Column B, showing text "
onclick= "window.status= ' Hello ', settimeout (' Clearword () ', 3000)" >
</form>
</body>

2. On the press, you??? To?? B list? Hello Word, pay attention to three seconds, the word is??? А?/p>

 

1.?? First, fix one? Name? The function of Clearword () is set out below:

Window.status= ""

??? With a cave? b column of the text (? See?? -20 of the Ming), the G-Clearword sash (),????? The text in column B.

2. This time the press?? It's settled?? The right rhyme? Work, use, separate, G.??? To be ashamed of, to work:

onclick= "window.status= ' Hello ', settimeout (' Clearword () ', 3000)"

3. The following settimeout () are set out below:

Yes, wait 3 seconds (3000 milliseconds), G. Sash Clearword ()? function

In the 2nd chapter, you see? How to make the window??? to the father? R from the entertainment _?⒁?? The window, if the look of the people do not??? window, son? Huan Fung _??? Urge ^ above??,? You?? One???? _?⒆ right window??, and the window? ⑨? Just??? Rain p?

 

3. No, settimeout. Sash ()
SetTimeout ()?? It's just, the sash? But me? Can use one?? Follow the way to a. SetTimeout () again? Young about there Huai? Just do it. Ditch drop 诙?? SetTimeout () sash? Second?? Again?? Hold Anthony??,?? The friend H go down,? SetTimeout () is???? Sash Little?/p>

 

?? -71 from the function of invoking 1

In the???, you see how to use the settimeout () to make the text box 刖 Figure 1, yes you can also? Set other rates, such as adding 5 every five seconds, or 1 every five seconds.

1. What's the use of the G-device? ⑹ the timeout3.htm in the disk?? The N case has the following heat?

<script>
x = 0
function Countsecond ()
{x = x+1
Document.fm.displaybox.value=x
SetTimeout ("Countsecond ()", 1000)
}
</script> <body Bgcolor=lightcyan text=red> <p> </br>

<form name=fm>
<input type= "text" name= "Displaybox" value= "0" size=4 >
</form>

<script>
Countsecond ()
</script>

</body>

2.???? Do you notice the ⑨ d in the text box?

3. What's your ⑦@?n case? Go to the hard drive, change some, such as x = X+5, or ⒌ group R? 000, see what? Anti-??? /p>

 

1.??? Yes??? Script, first?? Yes Countsecond ()??? function, second?? After that, in the?? All right? The @ function.

2. To take note of the following:

function Countsecond ()
{x = x+1
Document.fm.displayBox.value = X
SetTimeout ("Countsecond ()", 1000)
}

? Countsecond ()?? Yu? Just??? SetTimeout (),??? Method in a second then?? Countsecond (), Countsecond ()?? Yu Sheng?? SetTimeout (), so the fruit is Countsecond () per second, the Huai-Wei?/p>

3. In JavaScript, I?? is to use the??? Ways to make things happen? Sash? One of them?? Use is??? R?, another?? What is the purpose of the trip? The chapter??? Hsin-Yin?/p>

In the above method, the settimeout () is set for a second, but? g. Do you have another body function? So one?? The R? is slightly more than a second, for example, a point? Maybe only 58?? Follow?.

 

4. Set the L-piece to stop settimeout ()
SetTimeout () The Circle of the??? On the????, you see the tong of the text box? But me? Is there a way to get to the bottom of the hemorrhoids? Richest??? Arrive at Rice Tun O?? One of them?? The method is to use If...else to set a??? L, if TRUE is??? Sash settimeout (), if FALSE to stop.

For example, to make the top???? Counter jumps to 20 and stops, but ⒂ Boo P's function makes the following changes.

function Countsecond ()
{if (X < 20)
{x = x + 1
Document.displaySec.displayBox.value = X
SetTimeout ("Countsecond ()", 1000)
}
}

 

5. Minutes and seconds of counter
In front of the??, believe you already? W. How to use SetTimeout ()? You look at a????? The example.

 

?? -72?? R's Counter

In???, you want to set??? text box, a??? The other??? seconds,???? ⑨? Just do it. Prophecy @??? In the text box from the "Woo"? R.

1. What's the use of the G-device? ⑹ the timeout4.htm in the disk?? The N case has the following heat?

<script>
X=0
Y=-1

function Countmin ()
{y=y+1
Document.displaymin.displaybox.value=y
SetTimeout ("Countmin ()", 60000)
}

function Countsec ()
{x = x + 1
Z =x% 60
Document.displaysec.displaybox.value=z
SetTimeout ("Countsec ()", 1000)
}
</script>

<body Bgcolor=lightcyan text=red> <p> </br>

<table> <tr valign=top> <td> you in Ben? The??? R. Yes: </td>

<td> <form name=displaymin>
<input type= "text" name= "Displaybox" value= "0" size=4 >
</form> </td>
<td> points </td>

<td> <form name=displaysec> </td>
<td> <input type= "text" name= "Displaybox" value= "0" size=4 >
</form> </td>
<td> sec. </td> </tr> </table>

<script>
Countmin ()
Countsec ()
</script>
</body>

2. You pay attention to??? Bottom Chau D in the text box.

 

1.??? Yes??? function, a?? What is the point? In the second. In the??,? is only settimeout () operation, because chunks?? R has other more refined?? Method. (Note: What's the way?) R? K No eclipse _. )

2. Note the function of seconds:

function Countsec ()
{x = x + 1
z = x% 60
Document.displaysec.displaybox.value=z
SetTimeout ("Countsec ()", 1000)

}

?? is modulus (???, for example z = x% 60 means first line x/60, come out?? Bottom? Z???? For example 82 seconds, modulus is 22, so the text box?? @ shows 22 instead of 82.

3. What if you want to? I'm a 谇. By 0, such as 01, 02, 03, etc., the following methods can be used:

function Countsec ()
{x = x + 1
z = x% 60
if (Z < ten) {z = "0" + z}
Document.displaysec.displaybox.value=z
SetTimeout ("Countsec ()", 1000)
}

 

 


--------------------------------------------------------------------------------

10.2 cleartimeout ()
In the first one, you see? How to use settimeout () Xing ditch? g. The sash is lucky?? function,? A?? SetTimeout () has started to follow the work, I?? To make it stop?? Can I use Cleartimeout ()? Method

Cleartimout () has the following law: Cleartimeout (Timeoutid)

To use Cleartimeout (), I??? Set settimeout () R. Settimout () one?? ?,? name? is Timeoutid, me?? Stop the R, just use it? Timeoutid? is a hacking?? Since the name, but a lot of programs? T on the Timeoutid Lance? /p>

In the following example, the person??? Timeoutid, what's the name of "E"? Meter1 and Meter2, as follows:

Timeoutid

Meter1 = settimeout ("Count1 ()", 1000)
Meter2 = settimeout ("Count2 ()", 1000)

Use? Meter1 and Meter2, some timeoutid names, in the fixed cleartimeout () r, you can specify the δ sac?? SetTimeout () Effective, no?? _ And another?? The operation of the settimeout ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.