1

次のコードとその動作を試しましたが、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);
4

3 に答える 3

4

setInterval は、一意の ID を unsigned int ( uint) として返します。この 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 );
于 2013-03-09T19:50:37.520 に答える
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);
于 2013-03-09T19:51:48.920 に答える
0

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

それがスローする2つのイベント:

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

于 2013-03-10T01:48:44.077 に答える