The setTimeout funtion is a JavaScript code used to execute a code in particular time intervals.
These time intervals are called timing events.
There are two methods to set interval with JavaScript:-
- setTimeout(function, milliseconds)- It executes the code after the specified number of milliseconds.
- setInterval(function, milliseconds)- It repeats the execution of the code after a particular time of interval.
The two arguments mentioned in the parameter are function and milliseconds.
The function is callback function which is executed after the number of milliseconds(given time interval).
Milliseconds is the time interval after which we want our callback function to be executed.
Below is the code for the setTimeinterval:-
HTML-
See Below to see setTimeout effect
<br>
<br>
<div id="textwrapper">The text will be changed after 2 seconds</div>
<br>
<br>
<input type="button" id="showDiv" value="Click to see SetTimeout effect">
CSS-
#textwrapper{
background-color: #FFFF99;
color: #000;
display: none;
width: 30%;
/*margin: 0 auto;*/
padding: 10px 20px;
font-size: 20px;
}
input{
background-color: #FF5050;
color: #fff;
border:none;
padding: 10px 15px;
}
JAVASCRIPT-
$("#showDiv").click(function () {
$('#textwrapper').show(1000, function () {
setTimeout(function () {
$('#textwrapper').html(function () {
setTimeout(function () {
$('#textwrapper').html('Text is replaced');
}, 0);
setTimeout(function () {
$('#textwrapper').html('Again text is replaced');
}, 2000);
});
}, 2000);
});
});
You can check this example in below link:-
https://jsfiddle.net/oftdt6v3/
0 Comment(s)