@John-freeCodeCamp here
I’m not bypassing a ban. Just can’t login through github because it’s blocked on our school device.
I’ve been typing this throughtout the day by the way.
I posted this on stackoverflow, and as expected, just got disliked when people can’t awnser and try to call it a duplicate while linking me to a mostly unrelated topic.
This is just a basic clock. If you want to play around with the functionality, go to —> https://repl.it/@John_Nicole/Clock
Not totally the same, but will show you a base of what the code is doing. I stop instead of return. If you look closely, the code is really simple, just takes up a lot of space and is repetive.
Now i have some code. All it does is count down from a given time:
function clock(hours, minutes, seconds) {
var interval = setInterval(function(){
hours = Number(hours)
seconds = Number(seconds)
minutes = Number(minutes)
if (hours === 0 && minutes === 0 && seconds -1 < 0) {
return 1
}
$("#stop").on('click', function() {
clearInterval(interval)
})
if (minutes === 0 && seconds-1 < 0) {
if (hours < 10) {
hours = "0"+hours
}
// 24:00.000 - 1 second
hours-=1
minutes = 59
seconds = 59
time = hours+":59:59"
console.log(hours, minutes, seconds)
} else if (minutes > 0 && seconds -1 < 0) {
// 24:37.00 - 1 seconds
if (hours < 10) {
hours = "0"+hours
}
if (minutes < 10) {
minutes = "0"+minutes
}
minutes -=1
seconds = 59
console.log(hours, minutes, seconds)
} else {
// 24:37.49 - 1 second
if (hours < 10) {
hours = "0"+hours
}
if (minutes < 10) {
minutes = "0"+minutes
}
seconds-=1
if (seconds < 10) {
seconds = "0"+seconds
}
console.log(hours, minutes, seconds)
}
}, 1000);
}
Now if i say console.log(clock("01", "02", "00"))
i get undefined
. If you look in the code it shows:
if (hours === 0 && minutes === 0 && seconds -1 < 0) {
clearInterval(interval)
return 1
}
Basically when the time is 0, it should return 1. When i console.log() this function, it just returns undefined and starts the clock. This is instead of doing the function than returning. I am stopping the setInterval() than returning. Even without that it’s the same result.
Is there any idea how i can return in a setInterval and not get undefined after period of time? I am stopping the setInterval() than returning.