Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
101
rated 0 times [  105] [ 4]  / answers: 1 / hits: 5992  / 8 Years ago, fri, may 13, 2016, 12:00:00

I'm trying to set a timer so it display the seconds and min left of an user and I'm using setInterval to get the seconds and if there are 60 seconds it will reduce 1 min from the user.

The thing is that I'm getting infinite for loops every time I try to do it.


Something like


var userObj = {
name: "",
min: 0,
sec:0
}

function timerCount() {
while (userObj.sec !== 0) {
console.log(userObj.min)
if (userObj.sec == 0) {
setInterval(function() {
userObj.min--;
userObj.sec = 59
}, 1000);
}
while(userObj.sec !== 0) {
setInterval(function() {
console.log(userObj.sec)
userObj.sec--;
}, 1000);
}
}
}

More From » jquery

 Answers
5
var userObj = {
name: ,
min: 0,
sec:5
}

function timerCount() {

if(userObj.min === 0 && userObj.sec === 0) {
return;
}

if (userObj.sec === 0) {
userObj.min--;
userObj.sec = 59
}
userObj.sec--;


setTimeout(timerCount, 1000)
}

timerCount()


With little effort we can remove those while loops that are causing the issue. We also want to check for the minutes and seconds equalling 0 so we know when to end our timer.


[#28772] Wednesday, May 11, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
randall

Total Points: 492
Total Questions: 99
Total Answers: 103

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;