在这种情况下我能做什么?在 setinterval 倒数计时器内设置 setinterval
What can i do in this case? Setinterval within setinterval countdown timer
我需要有关在 setInterval 中使用倒计时的帮助或想法。
这是我在 js 上的倒数计时器(非常明显):
function startTimer(duration) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
document.getElementById('minuteDrag').innerText = minutes;
document.getElementById('secondDrag').innerText = seconds;
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
当我在第二个 setinterval 之外调用此函数时,它工作得很好(如预期的那样)。
但是当我在下面的代码中尝试 运行 这个函数时,时间倒计时的函数被窃听了,我猜这是因为在另一个 setinterval 中调用了倒数计时器。
有什么解决方案吗? (我无法退出“主要设置间隔”)。
var api = new Lolesports_API();
api.get_event_details('pt-BR', '105562529627228920')
.then(result_games => {
var game_id
result_games.event.match.games.forEach(game => game_id = game.id)
api.get_teams('pt-BR')
.then(result_teams =>{
var teams_dict = {}
result_teams.teams.forEach(team => {teams_dict[team.id] = team.code})
setInterval(function(){
api.get_window(game_id, get_lastest_date())
.then(result_window => {
var last_frame = result_window.frames[result_window.frames.length - 1];
var game_state = last_frame.gameState;
startTimer(60*5)
if (game_state == 'in_game') {
document.getElementById("gameState").style.backgroundColor = '#33cc33';
stopwatch_game.start();
} else if (game_state == 'paused') {
document.getElementById("gameState").style.backgroundColor = '#66a3ff';
stopwatch_game.pause();
} else {
document.getElementById("gameState").style.backgroundColor = '#ff8080';
stopwatch_game.pause();
}
//stopwatch_game.start();
})
}, 500)
})
})
PS:这不是完整的代码,为了不污染问题,我删除了很大一部分。
现在您每半秒开始一个新的倒计时间隔。
为什么不 return startTimer 中 setInterval 的值,以便它可以在必要时被外循环停止?
例如
startTimer = (duration) => {
let timer = duration, minutes, seconds;
return setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
document.getElementById('minuteDrag').innerText = minutes;
document.getElementById('secondDrag').innerText = seconds;
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
然后在您的主循环中,您可以使用 clearInterval
停止计时器...或者至少检查它是否存在:
let foo = undefined;
if (foo === undefined) {
foo = startTimer(60*5);
}
我需要有关在 setInterval 中使用倒计时的帮助或想法。
这是我在 js 上的倒数计时器(非常明显):
function startTimer(duration) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
document.getElementById('minuteDrag').innerText = minutes;
document.getElementById('secondDrag').innerText = seconds;
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
当我在第二个 setinterval 之外调用此函数时,它工作得很好(如预期的那样)。 但是当我在下面的代码中尝试 运行 这个函数时,时间倒计时的函数被窃听了,我猜这是因为在另一个 setinterval 中调用了倒数计时器。
有什么解决方案吗? (我无法退出“主要设置间隔”)。
var api = new Lolesports_API();
api.get_event_details('pt-BR', '105562529627228920')
.then(result_games => {
var game_id
result_games.event.match.games.forEach(game => game_id = game.id)
api.get_teams('pt-BR')
.then(result_teams =>{
var teams_dict = {}
result_teams.teams.forEach(team => {teams_dict[team.id] = team.code})
setInterval(function(){
api.get_window(game_id, get_lastest_date())
.then(result_window => {
var last_frame = result_window.frames[result_window.frames.length - 1];
var game_state = last_frame.gameState;
startTimer(60*5)
if (game_state == 'in_game') {
document.getElementById("gameState").style.backgroundColor = '#33cc33';
stopwatch_game.start();
} else if (game_state == 'paused') {
document.getElementById("gameState").style.backgroundColor = '#66a3ff';
stopwatch_game.pause();
} else {
document.getElementById("gameState").style.backgroundColor = '#ff8080';
stopwatch_game.pause();
}
//stopwatch_game.start();
})
}, 500)
})
})
PS:这不是完整的代码,为了不污染问题,我删除了很大一部分。
现在您每半秒开始一个新的倒计时间隔。
为什么不 return startTimer 中 setInterval 的值,以便它可以在必要时被外循环停止?
例如
startTimer = (duration) => {
let timer = duration, minutes, seconds;
return setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
document.getElementById('minuteDrag').innerText = minutes;
document.getElementById('secondDrag').innerText = seconds;
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
然后在您的主循环中,您可以使用 clearInterval
停止计时器...或者至少检查它是否存在:
let foo = undefined;
if (foo === undefined) {
foo = startTimer(60*5);
}