/    Sign up×
Community /Pin to ProfileBookmark

How to start my timer on page load?

Hi there,

I’ve got the following working timer:

[CODE]<html>

<head>

<title>Javascript Stopwatch (Cumulative Events)</title>

<script type=”text/javascript”>
var flagclock = 0;
var flagstop = 1;
var stoptime = 0;
var currenttime;
var splitdate = ”;
var output;
var clock;

function startstop()
{
var startstop = document.getElementById(‘startstoptimer’);
var startdate = new Date();
var starttime = startdate.getTime();
if(flagclock==0)
{
startstop.value = ‘Stop’;
flagclock = 1;
counter(starttime);
}
else
{
startstop.value = ‘Start’;
flagclock = 0;
flagstop = 1;
splitdate = ”;
}
}

function counter(starttime)
{
output = document.getElementById(‘output’);
clock = document.getElementById(‘clock’);
currenttime = new Date();
var timediff = currenttime.getTime() – starttime;
if(flagstop == 1)
{
timediff = timediff + stoptime
}
if(flagclock == 1)
{
clock.value = formattime(timediff,”);
refresh = setTimeout(‘counter(‘ + starttime + ‘);’,10);
}
else
{
window.clearTimeout(refresh);
stoptime = timediff;
}
}

function formattime(rawtime,roundtype)
{
if(roundtype == ’round’)
{
var ds = Math.round(rawtime/100) + ”;
}
else
{
var ds = Math.floor(rawtime/100) + ”;
}
var sec = Math.floor(rawtime/1000);
var min = Math.floor(rawtime/60000);
ds = ds.charAt(ds.length – 1);
if(min >= 60)
{
startstop();
}
sec = sec – 60 * min + ”;
if(sec.charAt(sec.length – 2) != ”)
{
sec = sec.charAt(sec.length – 2) + sec.charAt(sec.length – 1);
}
else
{
sec = 0 + sec.charAt(sec.length – 1);
}
min = min + ”;
if(min.charAt(min.length – 2) != ”)
{
min = min.charAt(min.length – 2)+min.charAt(min.length – 1);
}
else
{
min = 0 + min.charAt(min.length – 1);
}
return min + ‘:’ + sec;
}
</script>

<style type=”text/css”>

*

{

margin: 0;

padding: 0;

}

input, textarea

{

width: 100px;

font: normal 10pt verdana;

}

</style>

</head>

<body>

<input id=”clock” type=”text” value=”00:00″ style=”text-align: center;background-color:white;border:1px solid gray;font-weight:bold;font-size:14pt;” readonly><br>

<input id=”startstoptimer” type=”button” value=”Start” onClick=”startstop();” style=”font-weight:bold”><br>

</body>

</html>[/CODE]

All works great. At the moment I have to press the button to start the timer, but I want the timer to start automatically on page load (and keep the stop/start button to work of course, so the button has to show Stop instead of Start on page load).

Thanks

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@BIOSTALLMar 06.2011 — On your opening <body> tag could you add onload="startstop();"? So the code will look like so:

<body onload="startstop();">

Hope that helps
Copy linkTweet thisAlerts:
@NulllauthorMar 06.2011 — OMG THAT simple?? ? It worked, thanks a million!!!
Copy linkTweet thisAlerts:
@mitch6May 08.2019 — Hi there Nulll,

I was wondering if I could please use your timer and code in a project I am working on?

If so, does anyone know how to add a couple of features to it? (I am clueless with JS) I was wondering if I could change the background of the timer and the html table that the timer is in after a certain amount of time? Ie. from 0-4 mins timer is green, 4-8 timer is orange and after 8 mins timer is red. I would also like to stop the timer after 15 mins.

Thank you very much for your time and help.

Kind regards,

Mitch Baker
Copy linkTweet thisAlerts:
@mitch6May 08.2019 — @Nulll#1142416 Also, this was posted on my birthday 8 years ago xD
Copy linkTweet thisAlerts:
@SempervivumMay 08.2019 — @mitch6#1603421

I added the feature "change background color". You can adjust the limits easily; I used small values in order to be able to test it easily.

Regarding the feature "stop after 15 minutes": Should the start/stop button be disabled so that the timer can't be started again?
&lt;!doctype html&gt;
&lt;html&gt;

&lt;head&gt;

<i> </i>&lt;title&gt;Javascript Stopwatch (Cumulative Events)&lt;/title&gt;


<i> </i>&lt;style type="text/css"&gt;
<i> </i> * {
<i> </i> margin: 0;
<i> </i> padding: 0;
<i> </i> }

<i> </i> input,
<i> </i> textarea {
<i> </i> width: 100px;
<i> </i> font: normal 10pt verdana;
<i> </i> }
<i> </i>&lt;/style&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;table id="mytable"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input id="clock" type="text" value="00:00"
style="text-align: center;background-color:white;border:1px solid gray;font-weight:bold;font-size:14pt;"
readonly&gt;&lt;br&gt;
&lt;/td&gt;
&lt;td&gt;Another Cell&lt;/td&gt;
&lt;td&gt;Another Cell&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;input id="startstoptimer" type="button" value="Start" onclick="startstop();" style="font-weight:bold"&gt;&lt;br&gt;
&lt;script type="text/javascript"&gt;
var flagclock = 0;
var flagstop = 1;
var stoptime = 0;
var currenttime;
var splitdate = '';
var output;
var clock;
var thetable = document.getElementById("mytable");
// Adjust limits and colors here
// Specify limits in seconds
var limit1 = 20; color1 = "lightsalmon";
var limit2 = 40; color2 = "lightblue";
var limit3 = 60; color3 = "lightgreen";

<i> </i> function startstop() {
<i> </i> var startstop = document.getElementById('startstoptimer');
<i> </i> var startdate = new Date();
<i> </i> var starttime = startdate.getTime();
<i> </i> if (flagclock == 0) {
<i> </i> startstop.value = 'Stop';
<i> </i> flagclock = 1;
<i> </i> counter(starttime);
<i> </i> }
<i> </i> else {
<i> </i> startstop.value = 'Start';
<i> </i> flagclock = 0;
<i> </i> flagstop = 1;
<i> </i> splitdate = '';
<i> </i> }
<i> </i> }

<i> </i> function counter(starttime) {
<i> </i> output = document.getElementById('output');
<i> </i> clock = document.getElementById('clock');
<i> </i> currenttime = new Date();
<i> </i> var timediff = currenttime.getTime() - starttime;
<i> </i> if (flagstop == 1) {
<i> </i> timediff = timediff + stoptime
<i> </i> }
<i> </i> if (flagclock == 1) {
<i> </i> clock.value = formattime(timediff, '');
<i> </i> refresh = setTimeout('counter(' + starttime + ');', 100);
<i> </i> var secs = timediff / 1000;
<i> </i> var thecolor = "white";
<i> </i> if (secs &gt; limit3) thecolor = color3;
<i> </i> else if (secs &gt; limit2) thecolor = color2;
<i> </i> else if (secs &gt; limit1) thecolor = color1;
<i> </i> thetable.style.backgroundColor = thecolor;
<i> </i> console.log(timediff / 1000)
<i> </i> }
<i> </i> else {
<i> </i> window.clearTimeout(refresh);
<i> </i> stoptime = timediff;
<i> </i> }
<i> </i> }

<i> </i> function formattime(rawtime, roundtype) {
<i> </i> if (roundtype == 'round') {
<i> </i> var ds = Math.round(rawtime / 100) + '';
<i> </i> }
<i> </i> else {
<i> </i> var ds = Math.floor(rawtime / 100) + '';
<i> </i> }
<i> </i> var sec = Math.floor(rawtime / 1000);
<i> </i> var min = Math.floor(rawtime / 60000);
<i> </i> ds = ds.charAt(ds.length - 1);
<i> </i> if (min &gt;= 60) {
<i> </i> startstop();
<i> </i> }
<i> </i> sec = sec - 60 * min + '';
<i> </i> if (sec.charAt(sec.length - 2) != '') {
<i> </i> sec = sec.charAt(sec.length - 2) + sec.charAt(sec.length - 1);
<i> </i> }
<i> </i> else {
<i> </i> sec = 0 + sec.charAt(sec.length - 1);
<i> </i> }
<i> </i> min = min + '';
<i> </i> if (min.charAt(min.length - 2) != '') {
<i> </i> min = min.charAt(min.length - 2) + min.charAt(min.length - 1);
<i> </i> }
<i> </i> else {
<i> </i> min = 0 + min.charAt(min.length - 1);
<i> </i> }
<i> </i> return min + ':' + sec;
<i> </i> }
<i> </i>&lt;/script&gt;


&lt;/body&gt;

&lt;/html&gt;
Copy linkTweet thisAlerts:
@VITSUSAMay 08.2019 — Following videos will be more helpful to you to know how to start timer -

https://www.youtube.com/watch?v=u_6CqjQ-L8Q

https://www.youtube.com/watch?v=MabjuyWrDI4
Copy linkTweet thisAlerts:
@mitch6May 13.2019 — @Sempervivum#1603427 Thank you very much for your help Sempervivum. Yes, the buttons should be disabled and the timer stop at 15 minutes. It is for a fire station turnout system. So the theory is, that the fire trucks should have left the station within 15 minutes of the timer starting. Thanks again for your help, I appreciate it very much
Copy linkTweet thisAlerts:
@mitch6May 13.2019 — @VITSUSA#1603431 Thank you very much VITSUSA, I will be sure to check them out
Copy linkTweet thisAlerts:
@sattaMaktaresultMay 13.2019 — Nice Post It's very Nice.

Wish to see much more like this. Thanks for sharing me this information.

Get in touch with more info:-http://satta-matkaresult.com/dd-record-chart/
Copy linkTweet thisAlerts:
@mitch6May 31.2019 — Hi Sempervivum,

Sorry to be a pest. Is there anyway that the actual clock background can have the colours changed, rather than the space around the clock? Thanks again for your help
×

Success!

Help @Nulll spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...