Javascript: setTimeout() vs setInterval()

October 11, 2007 § 1 Comment

What is setTimeout():

It is a function that can execute other javascript statement AFTER x interval. The interval unit is millisecond.

Syntax:

setTimeout(“do.something();”, 1000); //Execute do.something() 1 second later.

What is setInterval():

It is a function that can execute other javascript statement EVERY x interval. The interval unit is millisecond.

Syntax:

setInterval(“do.somethingElse();”, 2000); //Execute do.somethingElse() every 2 seconds.

Notes:

Use clearTimeout() to cancel setTimeout(). Similarly, clearInterval() is used to cancel setInterval().

When calling “this” inside setTimeout() or setInterval(), it refers to window(global) as opposed to the object you are calling the function from.

References:

Advertisements

§ One Response to Javascript: setTimeout() vs setInterval()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Javascript: setTimeout() vs setInterval() at RAPD.

meta

%d bloggers like this: