/    Sign up×
Community /Pin to ProfileBookmark

Question about timer in java script

Hi. I need to count down from 10 to 0 and in every second i want it to be displayed on the screen, how will i do it? Thanks ?

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMar 13.2006 — <i>
</i>var counter = 10;
function countDown() {
var obj = document.getElementById("countdownarea");
obj.firstChild.data = counter;
counter--;
if(0 &lt;= counter) {
window.setTimeout("countDown[COLOR=Red][B]()[/B][/COLOR]", 1000);
}
return true;
}
window.onload = countDown;

[code=html]
Countdown: <span id="countdownarea">#</span>
[/code]
Copy linkTweet thisAlerts:
@Sleep_ModeauthorMar 13.2006 — I actually am coding the pairs game with javascript ( im also learning java script at that time ), and with the help of the forum, i did it in 1 day. But there should be a timer to count from 0 till the game ends...

I modified this code but it doesnt work. Why?

[code=html]var timeCount = 0;

function countDown() {
var obj = document.getElementById("countdownarea");
obj.firstChild.data = timeCount;

timeCount ++;
window.setTimeout("countDown", 1000);

return true;
}
window.onload = countDown;[/code]


and in the html:

[code=html]Countdown: <span id="countdownarea">#</span>[/code]
Copy linkTweet thisAlerts:
@Sleep_ModeauthorMar 13.2006 — And i also should write the number of pairs left each time...

I can not do this with document.write(), because when i do this, it writes to another page??? What should i do?
Copy linkTweet thisAlerts:
@phpnoviceMar 13.2006 — I modified this code but it doesnt work. Why?[/QUOTE]
Hard to tell -- if you provide no more information than just the code. One thing, though, if you're trying to run multiple functions off of the [B]onload[/B] event, then you need to take special considerations for this situation. The reason is, using this format, only one function can use the [B]onload[/B] event at a time.
Copy linkTweet thisAlerts:
@phpnoviceMar 13.2006 — And i also should write the number of pairs left each time...

I can not do this with document.write(), because when i do this, it writes to another page??? What should i do?[/QUOTE]

Use a similar method as the countdown does.
Copy linkTweet thisAlerts:
@Sleep_ModeauthorMar 13.2006 — Ok, i applied only one function to onload now. When i opened the page, it writes 0 and then it says error, so it doesnt work also?
Copy linkTweet thisAlerts:
@phpnoviceMar 13.2006 — Error on which line and what was the text of the error message?
Copy linkTweet thisAlerts:
@Wisest_GuyMar 13.2006 — You forgot the ():

window.setTimeout("countDown[B]()[/B]", 1000);
Copy linkTweet thisAlerts:
@phpnoviceMar 13.2006 — I sure did. I corrected it, in red, in original post -- though, IIRC, IE will run that anyway without the parens.
Copy linkTweet thisAlerts:
@Sleep_ModeauthorMar 13.2006 — yes. its working thank you soooo much ?!!
Copy linkTweet thisAlerts:
@Sleep_ModeauthorMar 13.2006 — my last question is :d :

how can i stop the timeout?
Copy linkTweet thisAlerts:
@phpnoviceMar 13.2006 — It doesn't need stopping. If you don't keep executing the [b]setTimeout()[/b] method, then the counter will not change anymore. So, you just need an if-test on it similar to what I had in the original code. For example... You could have a global variable that indicates whether the timer should keep running or not. Just test that global variable before executing the [b]setTimeout()[/b] method again.
Copy linkTweet thisAlerts:
@Sleep_ModeauthorMar 14.2006 — yes it doesnt need stopping you are right phpnovice ?

i finally finished my game. thanks all ?
Copy linkTweet thisAlerts:
@Wisest_GuyMar 14.2006 — If you really want to stop it:

Timer = setTimeout(Code,MilliSec);

clearTimeout(Timer);
×

Success!

Help @Sleep_Mode 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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