/    Sign up×
Community /Pin to ProfileBookmark

counter help

Well I’m new to javascript, and new to this forum ?

I’m trying to make a counter, that for now will count up to ten, first on the page displaying 1, then erasing the 1, and displaying a 2 and so on. However I’m having a lot of trouble doing this -.- for some reason it displays the 1, and then it stops ? help fixing this would be appreciated ?

[CODE]<html>
<body>

<script>
var count=0

function counter()
{
count++
document.write(count)
document.close()
}

for (var i=0; i<10; i++ )
{
setTimeout(“counter()”,1000)
}
</script>

</body>
</html>[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 07.2005 — Your script is wiping itself out with that [b]document.write[/b] in there. Though not a complete document, the following is more like that for which you're looking:
[code=html]
<html>
<head>
<script type="text/javascript">
<!--//
var count=-1

function counter()
{
count++;
document.getElementById("countee").innerHTML = count;
if (count < 10) setTimeout("counter()",1000);
}
//-->
</script>
</head>

<body onload="counter(); return true;">
<div id="countee"></div>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@suxxorauthorJun 07.2005 — thank you for that code. I'm assuming its supposed to display the number inside the div tags correct? however when the page loads, it outputs nothing, and I am not too familiar with the getElementById method ><
Copy linkTweet thisAlerts:
@phpnoviceJun 07.2005 — I tested what I posted. It works perfectly. You must have applied some changes -- for example, changing the page background to black -- and, as a result, you can't see anything. But, yes, the function outputs its value within the DIV tags. On page load, it outputs a zero and then every second it counts up to 10.
Copy linkTweet thisAlerts:
@suxxorauthorJun 07.2005 — I don't know why it wasn't working for me before, but it now, thank you ?!
Copy linkTweet thisAlerts:
@phpnoviceJun 07.2005 — Cheers.
×

Success!

Help @suxxor 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.18,
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,
)...