Skip to main content
Participating Frequently
July 5, 2014
Answered

how to loop if else statement

  • July 5, 2014
  • 1 reply
  • 602 views

Hello,

I want to make a open and close sign, I got to the part where I can make the MC visible or hide in certain time but I can not loop the statement.

Basically I want to show the open image from 10am to 8:30pm and the rest of the time show the close image.

Here is the code

var d:Date = new Date();

if ((d.getHours()>=10 && d.getHours()<8) || (d.getHours()==8 && d.getMinutes()<=30)){

open.visible = true;

close.visible = false;

} else {

open.visible = false;

close.visible = true;

}

trace(d)

It just read the time and show whatever the image should be, but it will not loop the whole statement again.

Can someone help me thank you.

This topic has been closed for replies.
Correct answer Ned Murphy

I don't think your conditional is going to give you what you want since 8 is less than 10, so if something is greater than 10 it cannot be less than 8.  The getHours value for 8PM is 20, not 8.

As far as looping goes, since this does not appear to be such a time-critical matter, you can get away with just using the Timer class to execute the code every minute or so rather than constantly checking every millisecond.  So create a Timer and have its event handler function process your conditional code.

var timer:Timer = new Timer(60000);

timer.addEventListener(TimerEvent.TIMER, checkTime);

function checkTime(evt:TimerEvent):void {

     // your if/else code goes here

}

timer.start();

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
July 5, 2014

I don't think your conditional is going to give you what you want since 8 is less than 10, so if something is greater than 10 it cannot be less than 8.  The getHours value for 8PM is 20, not 8.

As far as looping goes, since this does not appear to be such a time-critical matter, you can get away with just using the Timer class to execute the code every minute or so rather than constantly checking every millisecond.  So create a Timer and have its event handler function process your conditional code.

var timer:Timer = new Timer(60000);

timer.addEventListener(TimerEvent.TIMER, checkTime);

function checkTime(evt:TimerEvent):void {

     // your if/else code goes here

}

timer.start();

Participating Frequently
July 5, 2014

Thank you for your help, this is exactly what I want. But is there anyway to show the correct open or close image first (because both open and close image is visible at the same time) before the timer kick in? BTW thank you for pointing out 8pm is 20

I am guessing the code can be like this:

var d:Date = new Date();

if ((d.getHours()>=10 && d.getHours()<20) || (d.getHours()==20 && d.getMinutes()<=30)){

open.visible = true;

close.visible = false;

} else {

open.visible = false;

close.visible = true;

}

var timer:Timer = new Timer(60000);

timer.addEventListener(TimerEvent.TIMER, checkTime);

function checkTime(evt:TimerEvent):void {

var d:Date = new Date();

if ((d.getHours()>=10 && d.getHours()<20) || (d.getHours()==20 && d.getMinutes()<=30)){

open.visible = true;

close.visible = false;

} else {

open.visible = false;

close.visible = true;

}

}

Do you think that will work?

Thank again

Ned Murphy
Legend
July 5, 2014

If you modify the function slightly you can call it right at the start without waiting for the timer.  Add the red code highlighted below...

function checkTime(evt:TimerEvent=null):void {

    var d:Date = new Date();

    if ((d.getHours()>=10 && d.getHours()<20) || (d.getHours()==20 && d.getMinutes()<=30)){
          open.visible = true;
          close.visible = false;
    } else {
          open.visible = false;
          close.visible = true;
    }
}

checkTime();