HTML code redirect code

So, I did this code a while ago for random events on my site. The code works fine, but I wanted to add a timer.

What I need is when the user refreshes / enters the page, you are redirected, then I want him to redirect again to the same page every X seconds.

<script>
var test = Math.floor(Math.random() * 1) <= 1;

if (test) {
window.location = "URL";
}
</script>

      

+3


source to share


3 answers


Without timer:

If you want to redirect the user, you can use this line in your script:

window.location = "URL";

      

When a user enters or updates the site, redirect it.

With timer:



A) Once

Note . You can also use setTimeout

.

var timer = setInterval(function(){
    clearInterval(timer);
    window.location = "URL"     
},3000);

      

C) endless

setInterval(function(){
    window.location = "URL"     
},3000);

      

0


source


var seconds = 5

setTimeout(() => {
   window.location = "URL";
}, seconds * 1000)

      



Since you are reloading the page, you don't need the repetition interval. This timer will run every time the page is redirected.

0


source


You are probably looking for a tag meta refresh

:

<meta http-equiv="refresh" content="3;url=http://www.google.com/" />

      

Please note that the use of meta refresh is deprecated. You can visit this wiki for more information.

0


source







All Articles