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;
}
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;
}
Share
Improve this question
asked Sep 18, 2012 at 7:05
freakk69freakk69
1611 gold badge1 silver badge12 bronze badges
2
- do we really need to make it in a for loop? or we can do another way? – Netorica Commented Sep 18, 2012 at 7:09
- ok the answer of @janith is for you – Netorica Commented Sep 18, 2012 at 7:14
4 Answers
Reset to default 7What you need is the setInterval method:
setInterval(function(){
alert('hi');
},15*60*1000);
window.setInverval(function(){
alert("msg");
}, 1000*60*15);
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
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);