Two Methods for JS to Realize Automatic Jump After n Seconds
- 2021-07-01 06:27:19
- OfStack
In this paper, we share two methods for JS to realize automatic jump after n seconds for your reference. The specific contents are as follows
The first uses SetInterval:
$(function () {
setInterval(ChangeTime, 1000);
});
function ChangeTime() {
var time;
time = $("#time").text();
time = parseInt(time);
time--;
if (time <= 0) {
window.location.href = "/Home/Index";
} else {
$("#time").text(time);
}
}
The second uses SetTimeout:
$(function () {
setTimeout(ChangeTime, 1000);
});
function ChangeTime() {
var time;
time = $("#time").text();
time = parseInt(time);
time--;
if (time <= 0) {
window.location.href = "/Home/Index";
} else {
$("#time").text(time);
setTimeout(ChangeTime, 1000);
}
}
<div> There is an exception on the page you visited ,<span id="time">5</span> Seconds automatically skips to the home page </div>
The above is the whole content of this paper, hoping to help everyone learn javascript programming.