Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

jquery countdown timer problems

Featured Replies

Can anyone help? I'm only just learning javascript and have done pretty well so far but and having problems with a script for a 57 minute countdown.

function timerCallback () {
		var exit = false;
		timeCounter += 1000;  
		elapsed = Math.floor(timeCounter / 100) / 10;  
		var currQuestion = questionId + 1;
		if(timeLimit > 0) { // we use the time limit
			$("#" + prefix + "_quiz_timer").html("<span class='questioncounter'>" + currQuestion + "/" + questions.length + "</span> - " + fmtTime(timeLimit - elapsed));
			// make the clock red if last 10 seconds begin
			if(timeLimit - elapsed < 11) {
				$("#" + prefix + "_quiz_timer").css("color", "#F00");
			}
			if(timeLimit - elapsed <= 0) { // this is the end
				exit = true;
				// the previous button must be hidden here because quiz is at the end
				$("#" + prefix + "_prev").hide("slow");
				endQuiz();
			}
		} else {  // show only the elapsed time
			$("#" + prefix + "_quiz_timer").html("<span class='questioncounter'>" + currQuestion + "/" + questions.length + "</span> - " + fmtTime(elapsed));
		}
		// calculate the difference between the interval and the real elapsed interval
		var diff = (new Date().getTime() - startTime) - timeCounter;  
		if(exit == false) timerInstance = setTimeout(timerCallback, (1000 - diff));  
	};
	
	function fmtTime (value) {
		var hours = Math.floor(value / 360);
		var minutes = Math.floor((value - (hours * 360)) / 60);
		if(minutes < 10) minutes = "0" + minutes;
		var seconds = value % 60;
		if(seconds < 10) seconds = "0" + seconds;
		return hours + ":" + minutes +":"+ seconds;
	};

From the script above, linked to the .xml file i'm trying to get a 57 minute countdown but when entered in seconds (3420) the countdown starts at over 9 hour.

 

I've tried looking at various tutorials to try and work out what I've got wrong but cant seem to get anywhere. I've managed to get it to countdown from 5 minutes but anything over that won't display properly.

 

Thanks

Its mathematical problem. There are 3600 seconds in an hour, but for converting the value to hour you are dividing the value by 360, I think you should change it to 3600.

a date object represents it's time in milli seconds as it's smallest unit. now it takes 1000 of them to make a second and 60000 to make a minute and 360000000 to make an hour

so if you take

value = current time - start time ;

value will be represented in milliseconds

so to find the total amount of seconds passed

seconds_past=value / 1000;

if you want minutes past

minutes_past=value/ 60000

and if you want hours past

hours_past=value/360000000

 

or you can use the methods provided by the object

time_lapse=new Date() - start_time;

seconds_past=time_lapse.getSeconds(); /* they have this for hours and everything */

 

for a full review on the date object see HERE ( along with some examples )

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.