/    Sign up×
Community /Pin to ProfileBookmark

make a countdown time refresh every second

Hi everyone

I’m fairly new to javascript. I’ve created a script (below) that will countdown to a certain date i specify. However I’m having problems making the page refresh every second. If anyone has any advice on how i can do this or any tips to make my script more efficient i’d be very grateful!

<script language=”javascript” type=”text/javascript”>

var d = new Date();

var future = new Date(“September 20, 2007 18:30:00”);

var occur = (future – d);

var countdown = new Date(occur);
var cdate = countdown.getDate();
var chours = addzero(countdown.getHours());
var cmins = addzero(countdown.getMinutes());
var csecs = addzero(countdown.getSeconds());

function addzero(i) {
if (i<10)
{i=”0″ + i}
return i
}

document.write(“The next game will be in <br/>”)
document.write(cdate + ” Days : <br/>”);
document.write(chours + ” Hours : <br/>”)
document.write(cmins + ” Minutes : <br/>”)
document.write(csecs + ” Seconds <br/>”);

</script>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rohitchawlaSep 11.2007 — why u refresh the page

just make a text field and update the time in it every sec

put ur code in a function and use setInterval or setTimeout to call function every sec

put this in html body
[code=html]
<p>The next game will be in</p><br><input type="text" name="timer" id="timer" size=40>
<script language="javascript" type="text/javascript">
var future = new Date("September 20, 2007 18:30:00");
countdownclock(future);
</script>
[/code]


and use this function in head section

[CODE]
function countdownclock(future)
{
var d = new Date();
var occur = (future - d);
var countdown = new Date(occur);
var cdate = countdown.getDate();
var chours = addzero(countdown.getHours());
var cmins = addzero(countdown.getMinutes());
var csecs = addzero(countdown.getSeconds());
function addzero(i) {
if (i<10)
{i="0" + i}
return i
}
var thetime=cdate+":"+chours+":"+cmins+":"+csecs
document.getElementById("timer").value=thetime;
setTimeout("countdownclock("+future+")",500);
}
[/CODE]
×

Success!

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