如何正确使用 setInterval 和 clearInterval 在两个不同的函数之间切换
How do I correctly use setInterval and clearInterval to switch between two different functions?
为了练习,我试图显示一个从 0 - 9 递增的数字,然后从 9 - 0 递减,并无限重复.
到目前为止,我拥有的代码似乎很接近,但在第二次迭代时,我的 2 个相应函数的 setInterval
次调用似乎相互冲突countUp
和countDown
, 由于显示的数字未按预期顺序计算...然后浏览器崩溃.
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Algorithm Test</title>
</head>
<body onload = "onloadFunctions();">
<script type = "text/javascript">
function onloadFunctions()
{
countUp();
setInterval(countUp, 200);
}
var count = 0;
function countUp()
{
document.getElementById("here").innerHTML = count;
count++;
if(count == 10)
{
clearInterval(this);
countDown();
setInterval(countDown, 200);
}
}
function countDown()
{
document.getElementById("here").innerHTML = count;
count--;
if(count == 0)
{
clearInterval(this);
countUp();
setInterval(countUp, 200);
}
}
</script>
From 0 - 9, up and down: <div id = "here"></div>
</body>
</html>
您需要将
返回值从setInterval( ... )
捕获到变量中,因为这是对计时器的引用:
var interval;
var count = 0;
function onloadFunctions()
{
countUp();
interval = setInterval(countUp, 200);
}
/* ... code ... */
function countUp()
{
document.getElementById("here").innerHTML = count;
count++;
if(count === 10)
{
clearInterval(interval);
countUp();
interval = setInterval(countUp, 200);
}
}
@Claude,你是对的,我提出的另一个解决方案与原始代码太不同了。这是另一种可能的解决方案,使用setInterval
和切换功能:
function onloadFunctions() {
var count = 0;
var refId = null;
var target = document.getElementById("aux");
var countUp = function() {
target.innerHTML = count;
count ++;
if(count >= 9) {
window.clearInterval(refId);
refId = window.setInterval(countDown, 500);
}
}
var countDown = function() {
target.innerHTML = count;
count --;
if(count <= 0) {
window.clearInterval(refId);
refId = window.setInterval(countUp, 500);
}
}
refId = window.setInterval(countUp, 500);
}
clearInterval(this);
. 你不能这么做。 您需要保存 setInterval
的返回值。
var interval;
function onloadFunctions()
{
countUp();
interval = setInterval(countUp, 200);
}
var count = 0;
function countUp()
{
document.getElementById("here").innerHTML = count;
count++;
if(count == 10)
{
clearInterval(interval);
countDown();
interval = setInterval(countDown, 200);
}
}
function countDown()
{
document.getElementById("here").innerHTML = count;
count--;
if(count == 0)
{
clearInterval(interval);
countUp();
interval = setInterval(countUp, 200);
}
}
试试这个:
...
<body onload = "onloadFunctions();">
<script>
var cup, cdown; // intervals
var count = 0,
here = document.getElementById("here");
function onloadFunctions() {
cup = setInterval(countUp, 200);
}
function countUp() {
here.innerHTML = count;
count++;
if(count === 10) {
clearInterval(cup);
cdown = setInterval(countDown, 200);
}
}
function countDown() {
here.innerHTML = count;
count--;
if(count === 0) {
clearInterval(cdown);
cup = setInterval(countUp, 200);
}
}
</script>
From 0 - 9, up and down: <div id = "here"></div>
</body>
还可以创建对#here
元素的单个引用。始终使用===
而不是==
有很多
方法可以解决这个问题,以下是我的建议:
function onloadFunctions() {
var count = 0;
var delta = 1;
var target = document.getElementById("here");
var step = function() {
if(count <= 0) delta = 1;
if(count >= 9) delta = -1;
count += delta;
target.innerHTML = count;
window.setTimeout(step, 500);
}
step ();
}
PS:使用setTimeout
比setInteval
更安全。
/** Tools */
const log = require('ololog').configure({
locate: false
})
let count = 0
let interval__UP
let interval__DOWN
function countUp () {
count++
log.green('countUp(): ', count)
if (count == 5) {
clearInterval(interval__UP)
interval__DOWN = setInterval(function () {
countDown()
}, 1000)
}
}
function countDown () {
count--
log.red('countDown(): ', count)
if (count == 0) {
clearInterval(interval__DOWN)
interval__UP = setInterval(function () {
countUp()
}, 3000)
}
}
function start () {
countUp()
log.cyan('start()')
interval__UP = setInterval(function () {
countUp()
}, 2000)
}
start()
控制台日志显示它正在工作
相关文章:
- JQuery合并了keyup和focusout两个函数
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 递归|两个函数名
- 同一事物的两个函数,一个崩溃,另一个不崩溃,为什么
- 如何调用两个函数是一种html输入类型
- 广义化对两个性质起反作用的两个函数
- 两个函数之间的角度承诺
- 在keyup更改上运行两个函数
- 为什么这两个函数调用不相等
- 在 javascript 中通过继承调用两个函数
- 删除两个函数使用的全局变量
- 我需要将这两个函数从javascript更改为JQuery,任何人都可以告诉我如何转换
- 我需要在单击时运行两个函数.即使嵌套在一起,我也无法让它们都工作
- iMacros iim播放如何调用两个函数
- 尝试连接Angular.js或JavaScript中的两个函数返回
- 我的if和else-if逻辑中的问题以及两个函数的冲突
- 如何在onchange事件中的两个函数之间进行选择
- javascript中的函数重载使两个函数名称相同,但输入数字不同
- 包括javascript按键中的两个函数
- 在onload上调用两个函数不起作用