5
我正在尝试构建一个类似http://codepen.io/GeoffStorbeck/full/RPbGxZ/的番茄钟。秒的值随机地到达NaN,然后在开始'break'后恢复正常。番茄钟计时器:变量值为'NaN'
$('#circle a').click(function() {
var timer = $('.time > span').html();
timer = timer.split(':');
var minutes = timer[0]; //Value of minutes
var seconds = timer[1]; //Value of seconds
var settimer = setInterval(function() {
seconds -= 1;
console.log(seconds);
if (seconds < 0 && minutes != 0) {
minutes -= 1;
minutes = String(minutes);
seconds = 59;
} else if (seconds < 10 && seconds.length != 2)
seconds = '0' + seconds;
if (minutes < 10 && minutes.length < 2)
minutes = '0' + minutes;
$('.time > span').html(minutes + ':' + seconds);
//Start break when session is completed
if (minutes == 0 && seconds == 0) {
$('.upper').find('h1').text('BREAK');
var time = $('#break').find('span').text();
$('.time > span').html('0' + time + ':00');
$('#circle a').trigger("click"); //Start timer for break
}
}, 1000);
});
这里的链接到codepen http://codepen.io/ibrahimjarif/pen/wMKJWN
如何解决NaN的问题? 有没有更好的方法来实现这个?
一些提示:不要使用'parseInt'没有第二个参数('10')。使用'.toString()'而不是'String('...')'。使用数字_whole_time:'var minutes = Number(timer [0]),seconds = Number(timer [1]);',只能追加前导'0'或转换为字符串_ .html'功能。 – Xufox
@Xufox感谢您的输入。修复了代码。 – Ibrahim
@Xufox我应该删除问题,因为我解决了这个问题?我是新手。所以请建议。 – Ibrahim