Calling other methods directly in Ready will prompt for missing object errors, and the workaround is as follows:
Method 1. The extension of jquery can be used to solve this problem.
$ (document). Ready (function () {
$.extend ({show:function () {alert ("Ready"); } }); SetInterval ("Show ()", 3000); });
Method 2. Do not use quotation marks and parentheses when specifying functions that are timed out.
$ (function () {function Show () {alert ("Ready");} setinterval (show,3000);//Note the function name does not have quotation marks and brackets!
Use SetInterval ("Show ()", 3000); "Missing objects" will be reported
});
Difference:
SetTimeout ()
To execute an expression or function from a specified time delay after loading;
execute only once; use with Window.cleartimeout.
SetInterval ()
At execution time, it executes an expression or function from the loading of the page at a specified interval (functionally similar to a recursive function); used with Window.clearinterval.
Additional notes:
Both of these methods can be used to implement JavaScript after a fixed time period. But each has its own application scenario.
Method
In fact, settimeout and setinterval have the same syntax. They all have two parameters, one is the code string to execute, and one is the time interval in milliseconds, and the code executes after that time period.
However, the two functions are still different, setinterval after the execution of the code, after the fixed time interval, it will also automatically repeat the code, and settimeout only one time to execute the code.
Although it appears that settimeout can only be applied to actions in On-off mode, it is possible to repeatedly invoke settimeout by creating a function loop for repeated operations:
ShowTime ();
function ShowTime ()
{
var today = new Date ();
Alert ("The time is:" + today.tostring ());
SetTimeout ("ShowTime ()", 5000);
}
Once this function is called, the time is displayed every 5 seconds. If you use SetInterval, the corresponding code is as follows:
SetInterval ("ShowTime ()", 5000);
function ShowTime ()
{
var today = new Date ();
Alert ("The time is:" + today.tostring ());
}
The two methods may look very similar, and the results shown will be much the same, but the big difference is that the SetTimeout method does not execute the Showtime function every 5 seconds, and it executes the Showtime function 5 seconds after each call to settimeout. This means that if the body part of the Showtime function takes 2 seconds to execute, the entire function is executed every 7 seconds. But setinterval is not bound by the function that he calls, it simply repeats the function once every time.
If it is required to perform an action precisely after every fixed interval, it is best to use setinterval, and if you do not want to cause mutual interference with successive calls, especially if the call of a function requires heavy computation and long processing time, then it is best to use settimeout.
Using SetInterval and settimeout in jquery