Javascript setInterval() 不重复。仅工作 1 次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10048568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:38:10  来源:igfitidea点击:

setInterval() not repeating. Works only 1 time

javascriptjquerycss

提问by Garrett R

I'm trying to have a div's left property change by its self - one every second when your hovering over so I made this:

我试图让 div 的 left 属性自行更改 - 当您悬停时每秒更改一次,所以我做了这个:

$("div.scroll_left").hover(function(){
    var left_num = $('div.license_video').css("left")
    var left_num1 = parseInt(left_num, 10) - 1;
    var timerID = setInterval(alert(left_num1), 1000);
    //var timerID = setInterval(slideleft(left_num1), 1000);
},function(){
    clearInterval(timerID);
});
//function slideleft(left_num){
    //$('.license_video').css('left', left_num + "%");
//}

In theory you would think it repeat till you move your cursor off which clears the interval. When I hover over it does it one time and never repeats (there are no errors). Then when I hover off it gives a error "Uncaught ReferenceError: timerID is not defined"

从理论上讲,您会认为它会重复,直到您将光标移开以清除间隔。当我将鼠标悬停在它上面时,它会执行一次并且从不重复(没有错误)。然后,当我将鼠标悬停时,它会出现错误“未捕获的 ReferenceError:timerID 未定义”

回答by Quentin

setIntervalisn't working at all. You aren't passing it a function as the first argument.

setInterval根本不工作。您没有将函数作为第一个参数传递给它。

You are calling alertimmediatelyand trying to use it's return value as the function to repeat.

您正在alert立即调用并尝试使用它的返回值作为重复的函数。

var timerID = setInterval(function () { alert(left_num1) }, 1000);

回答by Matt Ball

So you've got two different problems here:

所以你在这里有两个不同的问题:

// (1) timerID needs to be defined in a scope accessible to both hover callbacks
var timerID = null;

$("div.scroll_left").hover(function(){
    var left_num = $('div.license_video').css("left")
    var left_num1 = parseInt(left_num, 10) - 1;

    // (2) Pass a *function* to setInterval
    timerID = setInterval(function () {
        alert(left_num1)
    }, 1000);
}, function(){
    clearInterval(timerID);
    timerID = null;
});

When you write

当你写

setInterval(alert(left_num1), 1000);
// or
setInterval(slideleft(left_num1), 1000);

you are passing the value returned by calling alert()or slideleft()(respectively) to setInterval. You are notpassing the function itself.

您将通过调用alert()slideleft()(分别)返回的值传递给setInterval. 您没有传递函数本身。

回答by Niet the Dark Absol

You are assigning nullto be the function to call. Why? Because you called alertand assigned its return value to the setInterval parameter.

您正在指定null要调用的函数。为什么?因为您调用alert并将其返回值分配给了 setInterval 参数。

Instead, use an anonymous function:

相反,使用匿名函数:

setInterval(function() {doStuff();},1000);

回答by KingRider

Very easy ;-) ... i love jquery, no matter a version... u need javascript pure is send pm me is can help.

很简单;-) ...我喜欢 jquery,无论是哪个版本...你需要纯 javascript 发送 pm 我是可以帮助的。

umavez = 0;
setInterval(function() {
 if (umavez == 0) {
      for (x=0;x<10;x++) { 
        $('div.test').append('<div>'+x+'</div>');
      }
    }
  umavez = 1;
}, 500);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="test">Linha:</div>