SetInterval and setinterval in javascript

Source: Internet
Author: User

SetInterval and setinterval in javascript

The setInterval function in javascript is mainly used to call the operation method at a certain interval when making an animation or other intermittent rendering (operation) effect.

The format of setInterval expressions is as follows:

SetInterval (fnname, time, par1, par2,... parn );

SetInterval (obj, fnname, time, par1, par2,... parn );

The first is the most common expression syntax. The fnname parameter can be a reference to an anonymous function or a function name. time is the set time interval for calling faname, in milliseconds, the default value is 10 ms, par1 ....... parn is an optional parameter that is passed to the faname method.

The second method is to use the syntax of the object method. The faname parameter is the method of the obj object, and other parameters are the same as the first syntax.

The following example is used for explanation:

// SetInterval (function () {alert ("I'm the setInterval method to print the result");}, 3000) // print once every 3 seconds // function alert1 () {alert ("I'm the setInterval method to print the result")} function alert2 (str) {alert (str);} setInterval (alert1, 3000); setInterval ("alert1 ()", 3000); setInterval (alert2, 3000, "I'm setInterval method printing result"); setInterval ("alert1 ()", 3000, "I'm setInterval method printing result "); // Object method writing method obj = new Object (); // create a new Object obj. alert1 = function () {alert ("I'm the setInterval method to print the result");} obj. alert2 = function (str) {alert (str);} setInterval (obj, alert1, 3000); setInterval (obj, alert2, 3000, "I'm the setInterval method to print the result ");

SetInterval is generally used with clearInterval. clearInterval is used to clear the call of the setInterval method. The clearInterval parameter is the return value of setInterval,

var timer = setInterval(obj,alert1,3000);clearInterval(timer);

The above is all the content of this article. I hope you will like it.

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.