The difference is that setinterval executes the code once every other specified time period, with repeatability. and settimeout only executes once after the call.
The following is a deep understanding of two functions through the establishment of functions and the automatic deletion of letters;
1. The establishment of the function
The establishment of settimeout:
Copy Code code as follows:
ShowTime ();
function ShowTime ()
{
var today = new Date ();
Alert ("The time is:" + today.tostring ());
SetTimeout ("ShowTime ()", 5000);
}
The Showtime function is not performed once for five seconds after the function is called
The establishment of SetInterval
Copy Code code as follows:
SetInterval ("ShowTime ()", 5000);
function ShowTime ()
{
var today = new Date ();
Alert ("The time is:" + today.tostring ());
}
Conclusion: Seemingly two functions have similar results, but the second function repeats the time until the page is closed.
Elimination of two functions:
Elimination of the use of settimeout
Cleartimeout () function; instance of the invocation:
Copy Code code as follows:
var timeoutprocess = settimeout ("alert (' goal! ')", 3000);
var stopgoallink = document.getElementById ("Stopgoallink");
Attacheventlistener (Stopgoallink, "click", Stopgoal, false);/join event function, parameter (target; event; called function; bubble)
function Stopgoal ()
{
Cleartimeout (timeoutprocess);
}
Elimination of SetInterval
Copy Code code as follows:
var timeoutprocess = settimeout ("alert (' goal! ')", 3000);
var stopgoallink = document.getElementById ("Stopgoallink");
Attacheventlistener (Stopgoallink, "click", Stopgoal, False), or join event function, parameter is (target; event; called function; bubbling)
function Stopgoal ()
{
Clearinterval (timeoutprocess);
}