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.