I would like to update the time delay for setIntervals from 3sec to 2sec and so on by deducting the timing everytime the hello is traced.
I have also trace the timing variable that it is deducting in the output panel, however the setInterval doesn't update its time delay???
Is there anyway to overcome this problem? Thanks in advance.
/////////////////////////////////////////////
var timing:Number = 3000;
setInterval(showMessage,timing);
function showMessage()
{
trace("hello");
timing -= 1000;
trace(timing);
}
/////////////////////////////////////////
// basic is that after every interval you have to stop and restart the interval
/////////////////////////////////////////////
var timing:Number = 3000;
// i am keeping the intervalId so that it will help me in clearing the interval
var intervalId:uint = setInterval(showMessage,timing);
function showMessage()
{
trace("hello");
timing -= 1000;
trace(timing);
// it cannot be negative, below condition will help to sort that
if (timing<=0)
{
timing = 1;
}
// stop the interval
clearInterval(intervalId);
// start the interval
intervalId = setInterval(showMessage,timing);
}
/////////////////////////////////////////
you should use the timer class. you can change its frequency directly:
var t:Timer=new Timer(3000,0);
t.addEventListener(TimerEvent.TIMER,showMessage);
t.start();
function showMessage(e:TimerEvent):void{
trace("hello");
if(t.delay>1000){
t.delay -= 1000;
} else {
| t.delay = 1; //<- this is a problematic frequency. |
}
trace(t.delay);
}
North America
Europe, Middle East and Africa
Asia Pacific