im trying to make a countdown script
The tricky part is the startTime comes in 00:00:00 format which is minutes:seconds:hundredeths
how do I convert the time var (startTime) to where I can Count minutes, seconds, ans hundredths
This is what I have so far (not much)
function startCountdown () {
var minutes = startTime.subst(0, 2);
var seconds = startTime.subst(3, 2);
var hundredths = startTime.subst(6,2);
setInterval(runClock, 10)
}
function runClock {
if(hundredths == 0) {
seconds--;
} else {
hundredths --;
}
if(seconds== 0) {
minutes--;
} else {
seconds--;
}
}
Why not stick a console.log call in the function and run it to see what it does.
You might want to add some code to stop the setInterval after minutes reaches zero so as to not end up in an infinite loop but closing the page without closing the console will allow you to see what the current code is doing.
so at least I dont get an error, but what confuses me is did I not set it up right to fire off the runClock method a .1 second intervals? Cause the console just shows 1 time?
var timeinterval = setInterval(runClock(minutes,seconds,hundredths), 10)
What is happening there, is that the return value from the runClock() function is being assigned to run every 10 milliseconds. The runClock function has no return value, so nothing much will happen other than the runClock function being run just the once when setting up the interval.
What you need to have happen, is to use a wrapper function, so that the minutes seconds and hundredths can then be passed to the runClock function.
function runClockWrapper(minutes, seconds, hundredths) {
return function closureWrapper() {
runClock(minutes, seconds, hundredths);
}
}
...
var timeinterval = setInterval(runClockWrapper(minutes,seconds,hundredths), 10)
What happens here is that the returned function is given to setInterval, which retains knowledge of the variables (via closure), so that each time the closureWrapper function is run, the runClock function can update the variables from one run to the next.
You still have many other issues to sort out though, so good luck.
shouldn’t that count down the variables, er looking at the if statement has me thinking I didn’t set it up right.
Can you help me with the logic there?
You’re going about it the wrong way, you can’t decrement the hour, minute, seconds like that if you want the timer to be realistic. Instead you need to get the startTime in milliseconds and then do some simple math on the elapsed time to turn milliseconds into hours,minutes,seconds,hundreths.
var startTime = null;
var start = function() {
startTime = new Date().getTime()
tick()
}
var tick = function() {
var time = new Date().getTime()
var ms = time - startTime
var hours = Math.floor((ms / (60 * 60 * 1000)));
var minutes = Math.floor((ms / (60 * 1000)) % 60);
var seconds = Math.floor((ms / 1000) % 60);
var hundreths = Math.floor((ms / 10) % 60);
console.log(hours, minutes, seconds, hundreths);
setTimeout(tick, 100)
}
start()
If the time comes in as minutes: seconds: hundredths (like 99:99:99)
How do I convert that to ms
I can split the string into three variables (minutes, seconds, hundredths) like
var minutes = startTime.substr(0, 2);
var seconds = startTime.substr(3, 2);
var hundredths = startTime.substr(6,2);
You will be extremely unlikely to finish (about 50,000 to 1 odds) if you’re checking that a millisecond measurement has a precise difference of zero.
Instead, check if it’s less than or equal to zero.