/    Sign up×
Community /Pin to ProfileBookmark

Hi guys,
I need a count down timer for my webpage. The site i am developing is about an online test where users give a test. After the timer reaches zero the page should automatically be redirected to another page. The format of the timer will be mm:ss. I tried a lot of codes but none of them could give me what I wanted.

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisMay 01.2007 — <script type="text/javascript">
var cnt = 2*60; // 2 minutes convert to seconds

function countdown() {
if (cnt == 0)
{alert("Bye!");
// window.location.href = "next.htm";
}
else
{min = Math.floor(cnt / 60);
sec = cnt - (min * 60);
if (sec < 10) {sec = "0" + sec;}
if (min < 10) {min = "0" + min;}
document.f.c.value = min + ":" + sec;
cnt--;
_timer = setTimeout("countdown()", 1000);
}
}

var _timer = setTimeout("countdown()", 1000); // tick
</script>
<form name="f" onsubmit="return false">
<input name="c" type="text">
</form>
Copy linkTweet thisAlerts:
@abhi_firewallauthorMay 01.2007 — Hi gil_davis,

Thanks a lot... This was what I was looking for... Can you do me another favour. The problem is that whenever i refresh the page the timer also is reset. Can you get me the ajax code for it or any other method for the problem.
Copy linkTweet thisAlerts:
@gil_davisMay 01.2007 — I don't see why you would care. If the user hits refresh, won't the test get erased anyway?

I don't know much about ajax, but the overhead of an ajax call every second would surely irritate the user.
Copy linkTweet thisAlerts:
@abhi_firewallauthorMay 02.2007 — I'll explain you whats going on in the test. there are 4 sections and the user selects the section from a drop down box. After selecting the page gets reloaded and the new set of ques come up. Also the timer gets reset. This is where i'm facing problrm.?
Copy linkTweet thisAlerts:
@gil_davisMay 02.2007 — Pass the time remaining using the form. If there is nothing ("") in the form, initialize the countdown with the max time.<script type="text/javascript">
var reqAry = new Array();
var cnt = 2*60; // 2 minutes convert to seconds
if (location.search.length > 0)
{requestForm = location.search.substr(1).split("&");
for (i=0; i<requestForm.length; i++)
{tmp = requestForm[i].split("=");
reqAry[tmp[0]] = tmp[1];}
if (reqAry["h"] != "")
{cnt = reqAry["h"] * 1;} // convert string to number
}

function showCount() {
min = Math.floor(cnt / 60);
sec = cnt - (min * 60);
if (sec < 10) {sec = "0" + sec;}
if (min < 10) {min = "0" + min;}
document.f.c.value = min + ":" + sec;
}

function countdown() {
if (cnt == 0)
{alert("Bye!");
// window.location.href = "next.htm";
}
else
{cnt--;
showCount();
_timer = setTimeout("countdown()", 1000);
}
}

function updateTime(frm) {
frm.h.value = cnt;
}

window.onload = showCount;
var _timer = setTimeout("countdown()", 1000); // tick
</script>
<form name="f" onsubmit="updateTime(this)">
<input name="c" type="text"><br>
<select name="s">
<option>One
<option>Two
<option>Three
<option>Four
</select><br>
<input type="submit" value="Submit">
<input name="h" type="hidden" value="">
</form>
Copy linkTweet thisAlerts:
@ricpMay 02.2007 — Out of interest Gil, and this might seem picky but it's not intended to be, when you don't close the tags in HTML you might want to mention that it's HTML4 you are validating on.

Even in HTML4, I would still advocate closing of all tags (like your open <option> tags), as it provides better readability.
Copy linkTweet thisAlerts:
@abhi_firewallauthorMay 02.2007 — Thanks again... Finally its all fine.. Just one more thing.. When i just refresh the page the timer goes back to the last passed value. Can you just give the code to detect when refresh is clicked. This is one last thing i need.
Copy linkTweet thisAlerts:
@gil_davisMay 02.2007 — Can you just give the code to detect when refresh is clicked.[/QUOTE]You can't tell the difference between a refresh and a submit. They both give you the search string. I don't know how you would do that. Sorry. ?
Copy linkTweet thisAlerts:
@abhi_firewallauthorMay 02.2007 — ok fine.. but thanks a lot for all that help once again
×

Success!

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

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

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