Skip navigation
Currently Being Moderated

setInterval not updating its time?

Dec 25, 2011 2:53 PM

Tags: #setinterval

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);

}

/////////////////////////////////////////

 
Replies
  • Currently Being Moderated
    Dec 25, 2011 9:21 PM   in reply to vincentccw

    You have to stop the interval and restart it.

     

    But seeing how you are in the AS3 section you should look into using a Timer instead.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 25, 2011 10:13 PM   in reply to vincentccw

    // 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);

    }

    /////////////////////////////////////////

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 25, 2011 10:17 PM   in reply to vincentccw

    Give it a try:

     

    var timing:Number = 3000;

     

    id=setInterval(showMessage,timing);

    function showMessage()

    {

             trace("hello");

              if (timing!=0){

                  clearInterval(id);

                  timing -= 1000;

                  id=setInterval(showMessage,timing);

              }

             trace(timing);

     

    }

     
    |
    Mark as:
  • kglad
    62,117 posts
    Jul 21, 2002
    Currently Being Moderated
    Dec 25, 2011 11:45 PM   in reply to vincentccw

    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);

            

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points