freakk69
freakk69

Reputation: 161

javascript loop to check for every 15minutes

Hi i am little bit confused to write this loop. It should alert for every fifteen minutes stating with 0 mins.

var i = 0;
var l = 900;
var m = 90000;
for (i=i; i<=m; i++){
    alert(i+l);
    i=i+l;
}

Upvotes: 1

Views: 2080

Answers (4)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76408

Using @janith's answer, I suspect your next question will be how do I stop an interval:

var intId = setInterval(function()
{
    alert('foo');
},15*60000);//assign to var
clearInterval(intId);//stops the interval

Or even better (and safer, without globals):

var intervalMgmt = (function(intId)
{
    var start = function(cb,time)
    {
        intId = setInterval(cb,time);
    };
    var stop = function()
    {
        clearInterval(intId);
    };
    return {start:start,stop:stop};
})();
intervalMgmt.start(function()
{
    console.log('foo');
},5000);//logs "foo" every 5 seconds
//some time later:
intervalMgmt.stop();//stops the interval

Upvotes: 2

Anoop
Anoop

Reputation: 23208

Use following code:

var i = 0;
var l = 900;
var m = 90000;

 var setIntervalConst = setInterval(function(){
      if(i > m){
       clearInterval(setIntervalConst ); return;
      }
      alert(i+l);
       i=i+l;
  },15*60*1000);

Upvotes: 0

Marc
Marc

Reputation: 6771

window.setInverval(function(){
    alert("msg");
}, 1000*60*15);

Upvotes: 3

janith
janith

Reputation: 1025

What you need is the setInterval method:

setInterval(function(){
    alert('hi');
},15*60*1000);

Upvotes: 7

Related Questions