Home  >  Article  >  Web Front-end  >  How to jump to the page after 5 seconds in javascript

How to jump to the page after 5 seconds in javascript

王林
王林Original
2021-10-27 16:05:3018113browse

Javascript method to jump to the page after 5 seconds: [var div = document.querySelector('div'); var time = 5; timer(); setInterval(timer, 1000) ...].

How to jump to the page after 5 seconds in javascript

#The operating environment of this article: windows10 system, thinkpad t480 computer.

In the development of many projects, we often need to realize the effect of automatic page jump. The following code realizes the effect of automatic page jump after 5 seconds. Let’s take a look!

The specific code is as follows:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div></div>
    <script>
        var div = document.querySelector(&#39;div&#39;);
        var time = 5;
        timer();
        setInterval(timer, 1000)

        function timer() {
            if (time == 0) {
                location.href = &#39;http://www.baidu.com&#39;
            } else {
                div.innerHTML = &#39;将在&#39; + time + &#39;秒后跳转到百度&#39;;
                time--;
            }
        }
    </script>
</body>

</html>

Recommended learning: javascript video tutorial

The above is the detailed content of How to jump to the page after 5 seconds in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn