Alex
Alex

Reputation: 537

Show JS Alert after 5 seconds

I am trying to show a JS alert after 5 seconds. Right now I have this code:

<script>
function init() {
    var count=5;
    var counter=setInterval(timer,1000);
        function timer(){
            count=count-1;
            if(count==0){
                alert("This is an alert")
                window.location = "http://www.example.com";      
                return;
            } 
        }
    }
    window.onload = init;
</script>

The problem is that it's not working right. There is some kind of little error I can't see in the code.

Upvotes: 2

Views: 17932

Answers (6)

Pedram
Pedram

Reputation: 16615

Try use function inside setTimeOut

setTimeout(function() {
  alert("This is an alert")
  window.location = "http://www.example.com";
}, 5000);

Upvotes: 0

user3434588
user3434588

Reputation:

Your code works fine. It's just that your init function is not being called on window load for some reason. Try directly calling your function or using document.body.onload or inserting the script tag at the end of your HTML file, in the body:

//I have a thing of cleaning up code
function init() {
  var count = 5;
  var counter = setInterval(timer, 1000);
  function timer(){
    count -= 1; //count = count-1 and count -= 1 are the same
    if (count === 0) { //use triple equals sign
      clearInterval(counter); //delete the interval when we are done with it
      alert("This is an alert"); //semi-colon
      window.location = "http://www.example.com";
      return;
    }
  }
}
//Instead of releying on a event, we can just call the function directly
init();
//Or you can check out the HTML for another way
<!DOCTYPE html>
<html>
  <body>
    <!--
    All you content will go here.
    And then the script will be placed right after it. It will be ran when the whole document is loaded.
    You can do something like this -->
    <script>/*function init() {...*/</script>
  </body>
</html>

Upvotes: 0

Binh LE
Binh LE

Reputation: 377

You can use the simple way is

$(document).ready({

setInterval(showAlert(), 5000);
});

function showAlert() {
alert("This is an alert");
window.location = "http://www.example.com";      
return;
enter code here

Note: 1 second is equal 1000, so you want this function run after 5 seconds you need set 5000.

Upvotes: 0

Try

// ---------------- Try
function init() {
    var count = 5; // set secconds
    var counter = setInterval(timer, 1000 * count);

    function timer() {
        alert("This is an alert")
        window.location = "http://www.example.com";
        //clearInterval(counter) // stop interval
    }

}

window.onload = init;

// --------------- Or

function init() {
    var count = 5; // set secconds
    var counter = setInterval(function() {
        alert("This is an alert")
        window.location = "http://www.example.com";
        //clearInterval(counter) // stop interval
    }, 1000 * count);
}

//window.onload = init;



// --------------- Or

function init() {
    var count = 5; // set secconds
    var counter = setTimeout(function() {
        alert("This is an alert")
        window.location = "http://www.example.com";
    }, 1000 * count);
}

//window.onload = init;

Upvotes: 0

brk
brk

Reputation: 50326

Your code is working fine .Check this JSFIDDLE

Show JS Alert after 5 seconds

where as you are using setInterval which mean the timer function will execute in every 1 second and it will keep on executing even after alerting. Check console from developer's tool

Not sure if you need setInterval or if you are actually looking for setTimeout

Upvotes: 1

aidanharris
aidanharris

Reputation: 157

Why are you using setInterval and maintaining a count variable to deduce when five seconds have passed?

Your code could be simplified using setTimeout. For example:

window.onload = setTimeout(function(){
    alert('This is an alert');
    window.location = 'http://www.example.com';
}, 5000);

Upvotes: 5

Related Questions