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

Is their a simple way to detect if the actual time (in hour) is near one of 4 hours?

Contributor ,
Jan 21, 2018 Jan 21, 2018

Copy link to clipboard

Copied

I've got an AS3 code that calculate and displays 4 tides (changing everyday). Example :

 

High 1 = 13h20

 

High 2 = 23h30

 

Low 1 = 05h30

 

Low 2 = 16h20

 

In code, I'm not gonna put here the calculation of the tides as it's too long and not necessary for this problem, but the variables are :

var $pmm:String;

var $pms:String;

var $bmm:String;

var $bms:String; 

High 1 = $pmm 

High 2 = $pms 

Low 1 = $bmm 

Low 2 = $bms

The app also displays the actual time

var my_timer:Timer=new Timer(1000);

my_timer.addEventListener(TimerEvent.TIMER, onTimer);

my_timer.start(); 

function onTimer(e:TimerEvent😞void {

now = new Date();

trace(now.hours + ":" + now.minutes);

}

Is there a simple way to detect if now.hours is closest to $pmm, $pms, $bmm or $bms when the user open the app ?

TOPICS
ActionScript

Views

500

Translate

Translate

Report

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

Enthusiast , Jan 22, 2018 Jan 22, 2018

Something like should work - it's a bit more manual, but with four variables I think it's fine:

var $pmm:String = "13h20";

var $pms:String = "23h30";

var $bmm:String = "05h30";

var $bms:String = "16h20";

var curClose:String = $pmm;

var curDelta:int = delta($pmm);

if(delta($pms) < curDelta){

curDelta = delta($pms);

curClose = $pms;

}

if(delta($bmm) < curDelta){

curDelta = delta($bmm);

curClose = $bmm;

}

if(delta($bms) < curDelta){

curDelta = delta($bms);

curClose = $bms;

}

function delta(time:String):int

{

var n:int

...

Votes

Translate

Translate
Advisor ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

first you must declare any variables in a function or the compiler will throw an error

also space are not allowed in variable name;

----------------------

var $pmm:String = High_1;

var $pms:String = High_2;

var $bmm:String = Low_1;

var $bms:String = Low_2;

// Put in an Array all tides time values from your file and convert it in seconds (As3):

// Note: this example is ok if your tide file is for one day with time in ascendant order

var tideTimeSelected:String = "unknown";

var myTextLoader:URLLoader = new URLLoader();

myTextLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e:Event):void {

     var tmpArr:Array;

     var tmpArrNext:Array;

     var myArrayOfLines:Array = e.target.data.split(/\n/);

     var now:Date = new Date();

     var currentTimeSec:uint = (now.getHours()* 3600) + (now.getMinutes()* 60) + now.getSeconds();

     var tideRow:uint;

     var tideRowNext:uint;

     var countRow:uint = myArrayOfLines.length;

     for(var u:uint=0;u<countRow;u++){

          tmpArr = myArrayOfLines.split("h");

          tideRowKey = "tide_"+myArrayOfLines;

          tideRow = myArrayOfLines[tideRowKey] = (tmpArr[0] * 3600) + (tmpArr[1] * 60);

          if((u+1) < countRow){

               tmpArrNext = myArrayOfLines[u+1].split("h");

               tideRowNext = (tmpArryNext[0] * 3600) + (tmpArrNext[1] * 60);

          }

          if(tideRowNext == null){

               if(currentTimeSec >= tideRow){

                    tideTimeSelected = tideRowKey;

                    break;

               }

          }else{

               if(currentTimeSec >= tideRow && currentTimeSec < tideRowNext){

                    tideTimeSelected = tideRowKey;

                    break;

               }

          }

          tideRowNext = null;

     }

}

function onTimer(e:TimerEvent):void {

     myTextLoader.load(new URLRequest("myText.txt"));

}

var my_timer:Timer = new Timer(3600000); // run every hour

my_timer.addEventListener(TimerEvent.TIMER,onTimer);

my_timer.start();

---------------------------

so you will get the variable name like "tide_xxx";

you can use on your logic like this[tideTimeSelected] in order to replace $pmm and others.

this example took me a little time to do it, so if you need more help please send a PM so I can make you a quote

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

LATEST

Something like should work - it's a bit more manual, but with four variables I think it's fine:

var $pmm:String = "13h20";

var $pms:String = "23h30";

var $bmm:String = "05h30";

var $bms:String = "16h20";

var curClose:String = $pmm;

var curDelta:int = delta($pmm);

if(delta($pms) < curDelta){

curDelta = delta($pms);

curClose = $pms;

}

if(delta($bmm) < curDelta){

curDelta = delta($bmm);

curClose = $bmm;

}

if(delta($bms) < curDelta){

curDelta = delta($bms);

curClose = $bms;

}

function delta(time:String):int

{

var n:int = (parseInt(time.substr(0,2)) * 3600) + (parseInt(time.substr(3,2)) * 60);

var now:Date = new Date();

var curSec:int = (now.hours * 3600) + (now.minutes * 60);

return Math.abs(curSec - n);

}

Votes

Translate

Translate

Report

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