【问题标题】:How do you stop setInterval() after certain number of iterations在一定次数的迭代后如何停止 setInterval()
【发布时间】:2013-03-09 19:46:46
【问题描述】:

我已经尝试过以下代码及其工作,但是当它达到 130 时我该如何停止?

var textValue:Number = 67.1;
var addValue:Number = .1;

my_txt.text = textValue.toString();

function counter(){
    textValue += addValue;
    my_txt.text = textValue.toString();
}

setInterval(counter, 10);

【问题讨论】:

    标签: actionscript-3 flash adobe


    【解决方案1】:

    setInterval 以无符号整数 (uint) 形式返回唯一 ID。您可以使用带有此 ID 的 clearInterval 来停止间隔。代码:

    var textValue:Number = 67.1;
    var addValue:Number = .1;
    var myInterval:uint;
    function counter(){
        textValue += addValue;
        my_txt.text = textValue.toString();
        if( textValue >= 130 ) {
            clearInterval(myInterval);
        }
    }
    myInterval = setInterval( counter, 10 );
    

    【讨论】:

      【解决方案2】:

      您可以使用 clearInterval 停止间隔。试试这个:

      var textValue:Number = 67.1;
      var addValue:Number = .1;
      
      my_txt.text = textValue.toString();
      
      function counter(){
          textValue += addValue;
          my_txt.text = textValue.toString();
          //check for end value
          if (textValue>=130)
          {
              //clear the interval
              clearInterval(intervalID);
          }
      }
      
      //store the interval id for later
      var intervalID:uint = setInterval(counter, 10);
      

      【讨论】:

      • 1084:语法错误:在程序结束之前需要右括号。
      【解决方案3】:

      由于您可能正在使用 actionscript 3,因此我建议您根本不要使用间隔。 Timer 对象可能会更好,因为它可以提供更好的控制,例如能够设置它在停止自身之前触发的次数,并且能够根据需要轻松启动、停止和重新启动计时器。

      使用 Timer 对象并为每个刻度添加事件侦听器的示例

      import flash.utils.Timer;
      import flash.events.TimerEvent;
      
      // each tick delay is set to 1000ms and it'll repeat 12 times
      var timer:Timer = new Timer(1000, 12);
      
      function timerTick(inputEvent:TimerEvent):void {
          trace("timer ticked");
      
          // some timer properties that can be accessed (at any time)
          trace(timer.delay); // the tick delay, editable during a tick
          trace(timer.repeatCount); // repeat count, editable during a tick
          trace(timer.currentCount); // current timer tick count;
          trace(timer.running); // a boolean to show if it is running or not
      }
      timer.addEventListener(TimerEvent.TIMER, timerTick, false, 0, true);
      

      控制定时器:

      timer.start(); // start the timer
      timer.stop(); // stop the timer
      timer.reset(); // resets the timer
      

      它抛出两个事件:

      TimerEvent.TIMER // occurs when one 'tick' of the timer has gone (1000 ms in the example)
      TimerEvent.TIMER_COMPLETE // occurs when all ticks of the timer have gone (when each tick has happened 11 times in the example)
      

      API 文档:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多