July 16, 201313 yr 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
July 18, 201313 yr 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.
July 20, 201313 yr 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