Skip to main content
FDTFQV65
Participant
October 25, 2018
Answered

Script for Timer (counting up from specific time, and with variable interims)

  • October 25, 2018
  • 1 reply
  • 1200 views

Dear all,

I have seen how brilliant and helpful you have been to users with some flash coding issues, and I hoped you may be able to help me.

I am creating an animation to display on a treadmill, including three counters which, rather than loop over a temporal cycle should continue infinitely. The 3 timers are:

(1) run timer starting from 46:00 minutes, resetting at 59:59.

(2) calories burning

(3) miles increase

2&3 being very similar and requiring only a variable of time delay between incremental figures. I hope you might be able to help me? Thanks for your time.

This topic has been closed for replies.
Correct answer FDTFQV65

Managed to cook up this for the Timer for HTML5 (below). Any chance to reset at 60:00?

var that = this;

this.initialMinutes = 46.4;// minutes here

this.totalTime = 0;

this.currentTime = 0;

this.interval = 0;

this.start = function()

{

that.startTimer(that.updateCallBack, that.endCallback);

};

this.updateCallBack = function()

{

that.setText();

};

this.startTimer = function(updateCallback, endCallback)

{

clearInterval(that.interval);

that.totalTime = that.minutesToMilliseconds(that.initialMinutes);

that.currentTime = that.totalTime;

that.setText();

that.setTime(updateCallback, endCallback);

};

this.setTime = function(updateCallback, endCallback)

{

that.interval = setInterval(function()

{

that.currentTime += 1000;

updateCallback();

if (that.currentTime == 60)

{

clearInterval(that.interval);

endCallback();

}

}, 100);//speed here

};

this.setText = function()

{

var time = that.timeCode(that.currentTime);

that.timeText.text = time.minutes + ":" + time.seconds;

};

this.minutesToMilliseconds = function(minutes)

{

return 1000 * 60 * minutes;

};

this.timeCode = function(milliseconds)

{

var seconds = Math.floor((milliseconds / 1000) % 60);

var strSeconds = (seconds < 10) ? ("0" + String(seconds)) : String(seconds);

var minutes = Math.round(Math.floor((milliseconds / 1000) / 60));

var strMinutes = (minutes < 10) ? ("0" + String(minutes)) : String(minutes);

return {seconds: strSeconds, minutes: strMinutes};

};

this.start();


The following codes are husky I'm sure, but they're currently working for HTML. All are based on the same fundamental code.

Code for Incremental Time starting whenever User chooses (Time):

var that = this;

this.initialMinutes = 46.4;                                             // display here

this.totalTime = 0;

this.currentTime = 0;

this.interval = 0;

this.start = function()

{

that.startTimer(that.updateCallBack, that.endCallback);

};

this.updateCallBack = function()

{

that.setText();

};

this.startTimer = function(updateCallback, endCallback)

{

clearInterval(that.interval);

that.totalTime = that.minutesToMilliseconds(that.initialMinutes);

that.currentTime = that.totalTime;

that.setText();

that.setTime(updateCallback, endCallback);

};

this.setTime = function(updateCallback, endCallback)

{

that.interval = setInterval(function()

{

that.currentTime += 1000;

updateCallback();

if (that.currentTime == 60)

{

clearInterval(that.interval);

endCallback();

}

}, 1000);                                                                          // rate of refresh here

};

this.setText = function()

{

var time = that.timeCode(that.currentTime);

that.timeText.text = time.minutes + ":" + time.seconds;

};

this.minutesToMilliseconds = function(minutes)

{

return 1000 * 60 * minutes;

};

this.timeCode = function(milliseconds)

{

var seconds = Math.floor((milliseconds / 1000) % 60);

var strSeconds = (seconds < 10) ? ("0" + String(seconds)) : String(seconds);

var minutes = Math.round(Math.floor((milliseconds / 1000) / 60));

var strMinutes = (minutes < 10) ? ("0" + String(minutes)) : String(minutes);

return {seconds: strSeconds, minutes: strMinutes};

};

this.start();

Code for Random Number (Heartrate):

var that = this;

this.initialMinutes = 10410;                                             // display here

this.totalTime = 0;

this.currentTime = 0;

this.interval = 0;

this.start = function()

{

that.startTimer(that.updateCallBack, that.endCallback);

};

this.updateCallBack = function()

{

that.setText();

};

this.startTimer = function(updateCallback, endCallback)

{

clearInterval(that.interval);

that.totalTime = that.minutesToMilliseconds(that.initialMinutes);

that.currentTime = that.totalTime;

that.setText();

that.setTime(updateCallback, endCallback);

};

this.setTime = function(updateCallback, endCallback)

{

that.interval = setInterval(function()

{

that.currentTime += 1000;

updateCallback();

}, 5535);                                                                           // rate of refresh here

};

this.setText = function()

{

var time = that.timeCode(that.currentTime);

that.timeText.text = time.minutes;

};

this.minutesToMilliseconds = function(minutes)

{

return 100 * 60 * minutes;

};

this.timeCode = function(milliseconds)

{

var seconds = Math.ceil(Math.random() * 5 + 130);

var minutes = Math.ceil(Math.random() * 5 + 130);

return {seconds, minutes};

};

this.start();

Code for Incremental Numbers (Calories):

var that = this;

this.initialMinutes = 10410;                                             // display here

this.totalTime = 0;

this.currentTime = 0;

this.interval = 0;

this.start = function()

{

that.startTimer(that.updateCallBack, that.endCallback);

};

this.updateCallBack = function()

{

that.setText();

};

this.startTimer = function(updateCallback, endCallback)

{

clearInterval(that.interval);

that.totalTime = that.minutesToMilliseconds(that.initialMinutes);

that.currentTime = that.totalTime;

that.setText();

that.setTime(updateCallback, endCallback);

};

this.setTime = function(updateCallback, endCallback)

{

that.interval = setInterval(function()

{

that.currentTime += 1000;

updateCallback();

if (that.currentTime == 60)

{

clearInterval(that.interval);

endCallback();

}

}, 100);                                                                                // rate of refresh here

};

this.setText = function()

{

var time = that.timeCode(that.currentTime);

that.timeText.text = time.minutes;

};

this.minutesToMilliseconds = function(minutes)

{

return 100 * 60 * minutes;

};

this.timeCode = function(milliseconds)

{

var seconds = Math.floor((milliseconds / 1000) % 60);

var strSeconds = (seconds < 10) ? ("0" + String(seconds)) : String(seconds);

var minutes = Math.round(Math.floor((milliseconds / 1000) / 60));

var strMinutes = (minutes < 10) ? ("0" + String(minutes)) : String(minutes);

return {seconds: strSeconds, minutes: strMinutes};

};

this.start();

Code for Incremental Numbers with Decimal (Miles):

var that = this;

this.initialMinutes = 61.3;                                                  // display here

this.totalTime = 0;

this.currentTime = 0;

this.interval = 0;

this.start = function()

{

that.startTimer(that.updateCallBack, that.endCallback);

};

this.updateCallBack = function()

{

that.setText();

};

this.startTimer = function(updateCallback, endCallback)

{

clearInterval(that.interval);

that.totalTime = that.minutesToMilliseconds(that.initialMinutes);

that.currentTime = that.totalTime;

that.setText();

that.setTime(updateCallback, endCallback);

};

this.setTime = function(updateCallback, endCallback)

{

that.interval = setInterval(function()

{

that.currentTime += 1000;

updateCallback();

if (that.currentTime == 10)

{

clearInterval(that.interval);

endCallback();

}

}, 4535);                                                                                // rate of refresh here

};

this.setText = function()

{

var time = that.timeCode(that.currentTime);

that.timeText.text = time.minutes + "." + time.seconds;

};

this.minutesToMilliseconds = function(minutes)

{

return 1000 * 10 * minutes;

};

this.timeCode = function(milliseconds)

{

var seconds = Math.floor((milliseconds / 1000) % 100);

var strSeconds = (seconds < 10) ? ("0" + String(seconds)) : String(seconds);

var minutes = Math.round(Math.floor((milliseconds / 1000) / 100));

var strMinutes = (minutes < 1) ? ("0" + String(minutes)) : String(minutes);

return {seconds: strSeconds, minutes: strMinutes};

};

this.start();

1 reply

Legend
October 25, 2018

Today I learned there are treadmills where the screen is actually displaying a web browser.

Since you tagged this as HTML, i assume you're working in an HTML5 Canvas document. That means JavaScript. So just use the JavaScript function for this:

WindowOrWorkerGlobalScope.setInterval() - Web APIs | MDN

FDTFQV65
FDTFQV65Author
Participant
October 25, 2018

Thank you. Perhaps I may disturb you by admitting that I'm not entirely sure how to apply this. There is very little scope to my knowledge, and I would be humbled if anyone could provide a line-by-line code for the three dynamic texts. Forgive me!

FDTFQV65
FDTFQV65Author
Participant
October 25, 2018

PS. I am flexible. Try me at HTML or ActionScript, I will rise to the challenge. It will be exported as OAM for Adobe Muse imbedding.