How to Redirect a Page with Timer in JavaScript

How to Redirect a Page with Timer in JavaScript


In this tutorial we will create a How to Redirect a Page with Timer in JavaScript. This tutorial purpose is to make redirecting timer that will open to a targeted website. This will tackle the basic functionality for the redirecting. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any of your on working website that needed redirecting function. I will try my best to give you the easiest way of creating this program Redirecting Timer. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will display only the string that will display some specific information. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="widht=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Redirect a Page with Timer in JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;">
  16. <center><h4>You are redirecting to sourcecodester website in <span id="timer" class="text-primary">10</span> seconds</h4></center>
  17. </div>
  18. </body>
  19. <script src="script.js"></script>
  20. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will automatically start the timer and redirect you to another website when the timer expire. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let time = 10;
  2. let counter;
  4. function countDownTimer() {
  5. let display = document.getElementById("timer");
  6. display.innerHTML = time;
  9. time--;
  11. if (time < 0) {
  12. clearInterval(counter);
  14. window.location.href = "";
  15. }
  16. }
  18. function startTimer() {
  19. counter = setInterval(countDownTimer, 1000);
  20. }
  23. startTimer();

In this code we first create a variable called time and set the value to 10. We also create an empty variable called counter that we will use to set a value. We will now create a method called countDownTimer(). In this method we will first bind the html text that will act as a timer naming display and set the value of the time on it.

To start our timer we will decrement first the time value, and set it to a condition when it reach zero it will call a specific function. The first we use is the clearInterval() this will destroy the timer when the timer expire and then we use window.location.href to redirect the page to a targeted website. Lastly we will create a method called startTimer() that will only set the interval of timer.


The How to Redirect a Page with Timer in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Redirect a Page with Timer in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment