Targeting dynamic text fields from within a function (JS)

New Here ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

I have the function below, it runs on a timer every second. The idea is to update the 3 dynamic text fields with the current hour, minute and second.  If the code inside the function is run on the root timeline, it's fine. But once placed within a function, the "this." parts that target the dynamic text fields (near the end of the function) no longer work. I guess the text fields are not visible to the function?   I've tried removing "this". I've tried adding "window." or "parent." but to no success. What targets *anything* on the root timeline from within a function?

 

function updatetime() {
totalmilli = totalmilli-1000;
console.log('Remaining milliseconds = ' + totalmilli)
// 1- Convert to seconds:
seconds = totalmilli / 1000;
// 2- Extract hours:
hours = parseInt( seconds / 3600 ); // 3,600 seconds in 1 hour
seconds = seconds % 3600; // seconds remaining after extracting hours
// 3- Extract minutes:
minutes = parseInt( seconds / 60 ); // 60 seconds in 1 minute
// 4- Keep only seconds not extracted to minutes:
seconds = seconds % 60;
// Handle display of the above.
if (seconds< 10){
seconds = "0"+seconds;
}
if (minutes< 10){
minutes = "0"+minutes;
}
if (hours< 10){
hours = "0"+hours;
}
console.log(hours+" hours and "+minutes+" minutes and "+seconds+" seconds!" );
this.dyn_hours.text = hours;
this.dyn_minutes.text = minutes;
this.dyn_seconds.text = seconds;
}

Views

26

Likes

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
New Here ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

LATEST

Ah, I eventually found the right thing to ask Google. Hurrah. The new equivalent of _root is exportRoot.

So 

 

exportRoot.dyn_hours.text = hours;

 

works.

Likes

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