Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

how to loop if else statement

Community Beginner ,
Jul 04, 2014 Jul 04, 2014

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.

TOPICS
ActionScript
533
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 05, 2014 Jul 05, 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 co

...
Translate
LEGEND ,
Jul 05, 2014 Jul 05, 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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 05, 2014 Jul 05, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 05, 2014 Jul 05, 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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 05, 2014 Jul 05, 2014

Appreciated your help this code works perfectly:

var timer:Timer = new Timer(60000);

timer.addEventListener(TimerEvent.TIMER, checkTime);

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

timer.start();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 05, 2014 Jul 05, 2014

Sorry to bothering you again, can you show me how can I make 2 set of timers.

example:

1. from Monday - Friday open at 10am - 8:30pm

2. from sataday - sunday open at 9am - 8:30pm

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 05, 2014 Jul 05, 2014

You don't need two timers, you need to add more to the conditional.  If you want to differentiate for different days look at using the getDay() method of the Date class to determine if it M-F or not.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 05, 2014 Jul 05, 2014

Yes, I will look into it. Thank you very much

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 06, 2014 Jul 06, 2014

Can you look into this code for me and tell me why I got this error:

Scene 1, Layer 'Layer 7', Frame 11084: Syntax error: expecting rightbrace before end of program.

var timer:Timer = new Timer(60000);

timer.addEventListener(TimerEvent.TIMER, checkTime);

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

var d:Date = new Date();

if (d.getDay()>=1 && d.getDay()<=5){

  if (d.getHours()>=11 && d.getHours()<=20){

  Open.visible = true;

  Close.visible = false;

  }

}

else

{

if (d.getDay()=6 || d.getDay()<=0){

  if (d.getHours()>=9 && d.getHours()<=20){

  Open.visible = false;

  Close.visible = true;

  }

}

}

checkTime();

timer.start();

I want to show M-F 11am to 8pm open else close

sat-sun 9am - 8pm open else close

Thank you for your help

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 06, 2014 Jul 06, 2014
LATEST

You can reduce your logic if you think about it... if the first day check is not true, then there is no need to test if it's day 6 or 0 because it has to be 6 or 0.  There are other potential simplifications you could reason out as well, for instance, it might (or might not) help to consider that for every day of the week the open image should be visible from 11AM to 8PM.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 06, 2014 Jul 06, 2014

Murphy,

I think I got the code corrected this time can you see if anything with the code this time, I really appreciate your help.

var timer:Timer = new Timer(60000);

timer.addEventListener(TimerEvent.TIMER, checkTime);

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

var d:Date = new Date();

if (d.getDay()>=1 && d.getDay()<=5){

  if (d.getHours()>=11 && d.getHours()<=20){

  open.visible = true;

  close.visible = false;

  }

}

else if (d.getDay()==6)

  {

  if (d.getHours()>=9 && d.getHours()<=20){

  open.visible = true;

  close.visible = false;

  }

}

else if (d.getDay()==0)

  {

  if (d.getHours()>=9 && d.getHours()<=20){

  open.visible = true;

  close.visible = false;

  }

}

else

{

open.visible = false;

close.visible = true;

}

}


checkTime();

timer.start();

Thank you so much!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines