/    Sign up×
Community /Pin to ProfileBookmark

start stop countdown -please help !

I cant seem to get this too work –

here is the original code – i want it count 30-0 then repeat 30-0 firing an alert 1 and an alert 2 reaching 0

promblem is it doesnt auto start counting

i just want the buttons to stop count and restart count

where you stopped it (ie 15 it will restart counting at 15 )

<html>
<head>
<script type=”text/javascript”>
var c=0;
var t;
var timer_is_on=0;

function timedCount()
{
document.getElementById(‘txt’).value=c;
c=c+1;
t=setTimeout(“timedCount()”,1000);
}

function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}

function stopCount()
{
clearTimeout(t);
timer_is_on=0;
}
</script>
</head>
<body>

<form>
<input type=”button” value=”Start count!” onclick=”doTimer()” />
<input type=”text” id=”txt” />
<input type=”button” value=”Stop count!” onclick=”stopCount()” />
</form>

</body>
</html>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@burnmeJul 22.2012 — I have absolutely NO clue what you said you were trying to do, but your code works, it starts and stops with the functions. Here is code for it to alert on 15 secs, you can add another for 30 if you like, but if you want more help, please restate the question.

[CODE]
<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;

function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;

if(c == 15){ alert("We have reached: " + c); }

t=setTimeout("timedCount()",1000);
}

function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}

function stopCount()
{
clearTimeout(t);
timer_is_on=0;
}
</script>
</head>
<body>

<form>
<input type="button" value="Start count!" onclick="doTimer()" />
<input type="text" id="txt" />
<input type="button" value="Stop count!" onclick="stopCount()" />
</form>

</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@Dava1986Jul 22.2012 — only thing missing from this is
[CODE]<body onload="doTimer()">[/CODE]
Copy linkTweet thisAlerts:
@George88Jul 22.2012 — You're better off using setInterval. It works the same way as setTimeout, but executes continuously until you call clearInterval; removing the recursive function calling you have going on. I've taken the liberty of rewriting your code. It works as you described. If you want to alert a value counting the number of resets, just add some form of counter to the code.

&lt;!DOCTYPE html&gt;

&lt;html&gt;
&lt;head&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;form&gt;
&lt;input type="button" value="Start count!" id="start_timer" /&gt;
&lt;input type="text" id="txt" value="0" /&gt;
&lt;input type="button" value="Stop count!" id="stop_timer" /&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;


&lt;script type="text/javascript"&gt;

window.addEventListener("load", begin_time, false);
document.getElementById("start_timer").addEventListener("click", restart_time, false);
document.getElementById("stop_timer").addEventListener("click", stop_time, false);

var time = 0;

function begin_time()
{
time = setInterval("count()", 1000);
}

function count()
{
document.getElementById("txt").value++;

<i> </i>if(document.getElementById("txt").value == 31)
<i> </i>{
<i> </i> document.getElementById("txt").value = 0;
<i> </i> alert("something...");
<i> </i>}
}

function restart_time()
{
begin_time();
}

function stop_time()
{
clearInterval(time);
document.getElementById("txt").value = 0;
}

&lt;/script&gt;
×

Success!

Help @jase 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 5.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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