ATG
ATG

Reputation: 1

Converting old AS2 script to AS3

I have just got my self into coding however I'm still learning and after searching everywhere last week I have given up.

I have a an old AS2 sript that I have compiled from various sources and created a count up clock for a friend website. Now the issue I have is that we all know flash will not be supported for much longer and is currently being phased out. Since this is the case can someone please convert the following code to AS3 or perhaps into JavaScript.

I have tried looking at countdown timers in AS3 but for the life of me I can't figure this out.

I hope someone can help of point me in the right direction.

Thank you for your help in advance.

// set date variables
var year = 1961;
var month = 3; // (number -1 as zero indexed array)
var day = 28;
var hour = 16; // (24 hour)
var minute = 40;
var second = 0;
start = new Date(year, month, day, hour, minute, second);

function calcAge() {
var now = new Date();
var o = new Object();

o.calendar_years = now.getYear()-start.getYear();
o.calendar_months = now.getMonth()-start.getMonth();
o.calendar_days = now.getDate()-start.getDate();
o.calendar_hours = now.getHours()-start.getHours();
o.calendar_minutes = now.getMinutes()-start.getMinutes();
o.calendar_second = now.getSeconds()-start.getSeconds();

if (o.calendar_second<0) {
    o.calendar_second += 60;
    o.calendar_second--;
}
if (o.calendar_minutes<0) {
    o.calendar_minutes += 60;
    o.calendar_hours--;
}
if (o.calendar_hours<0) {
    o.calendar_hours += 24;
    o.calendar_days--;
}
if (o.calendar_days<0) {
    // Days need to be taken from first day of the month
    var nextmonth = new Date(now.getYear(), now.getMonth(), 32);
    o.calendar_days += 32-nextmonth.getDate();
    o.calendar_months--;
}
if (o.calendar_months<0) {
    o.calendar_months += 12;
    o.calendar_years--;
}
return o;
}

function getAge(){
age = calcAge();
years_txt.text = age.calendar_years;
months_txt.text = age.calendar_months;
day_txt.text = age.calendar_days;
hour_txt.text = age.calendar_hours;
mins_txt.text = age.calendar_minutes;
sec_txt.text = age.calendar_second;
}

var intervalID = setInterval( getAge, 1000 );

Upvotes: 0

Views: 97

Answers (1)

Anthony Pace
Anthony Pace

Reputation: 462

Your code is already pretty close to javascript, as AS2/AS3 are ECMA Script based, and thus pretty transferable.

The major issue, is that you are updating multiple text objects that don't exist.

I changed your code so it assigns the text to the innerHTML of a div with the id = "ageOutDiv".

I also changed the name of the function getAge to outputAge, because you aren't returning / "getting" a value.

// set date variables
var year = 1961;
var month = 3; // (number -1 as zero indexed array)
var day = 28;
var hour = 16; // (24 hour)
var minute = 40;
var second = 0;

var ageOutDiv = document.getElementById('ageOutDiv');

start = new Date(year, month, day, hour, minute, second);

function calcAge() {
  var now = new Date();
  var o = new Object();

  o.calendar_years = now.getYear()-start.getYear();
  o.calendar_months = now.getMonth()-start.getMonth();
  o.calendar_days = now.getDate()-start.getDate();
  o.calendar_hours = now.getHours()-start.getHours();
  o.calendar_minutes = now.getMinutes()-start.getMinutes();
  o.calendar_second = now.getSeconds()-start.getSeconds();

  if (o.calendar_second<0) {
      o.calendar_second += 60;
      o.calendar_second--;
  }
  if (o.calendar_minutes<0) {
      o.calendar_minutes += 60;
      o.calendar_hours--;
  }
  if (o.calendar_hours<0) {
      o.calendar_hours += 24;
      o.calendar_days--;
  }
  if (o.calendar_days<0) {
      // Days need to be taken from first day of the month
      var nextmonth = new Date(now.getYear(), now.getMonth(), 32);
      o.calendar_days += 32-nextmonth.getDate();
      o.calendar_months--;
  }
  if (o.calendar_months<0) {
      o.calendar_months += 12;
      o.calendar_years--;
  }
  return o;
}

function outputAge(){
  age = calcAge();
  var str = "";
  str+="Year: "+age.calendar_years+"<br>";
  str+="Month: "+age.calendar_months+"<br>";
  str+="Days: "+age.calendar_days+"<br>";
  str+="Hours:"+ age.calendar_hours+"<br>";
  str+="Minutes:"+age.calendar_minutes+"<br>";
  str+="Seconds:"+age.calendar_second;
  ageOutDiv.innerHTML=str;
}

var intervalID = setInterval(outputAge,1000);
<div id="ageOutDiv"></div>

Upvotes: 0

Related Questions