Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setInterval() not repeating. Works only 1 time

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.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"

like image 774
Garrett R Avatar asked Apr 06 '12 19:04

Garrett R


People also ask

How do you repeat setInterval?

Use setInterval() to specify a function to repeat with a time delay between executions. Again, two parameters are passed to setInterval() : the function you want to call, and the amount of time in milliseconds to delay each call of the function . setInterval() will continue to execute until it is cleared.

How does the setInterval () function work in?

setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .

Why is setInterval not accurate?

why is setInterval inaccurate? As we know, setTimeout means to run the script after the minimum threshold (MS unit), and setInterval means to continuously execute a specified script with the minimum threshold value period. Note that I use the term minimum threshold here because it is not always accurate.

Does the setInterval () function work in JavaScript?

JavaScript setInterval() method. The setInterval() method in JavaScript is used to repeat a specified function at every given time-interval. It evaluates an expression or calls a function at given intervals. This method continues the calling of function until the window is closed or the clearInterval() method is called ...


2 Answers

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

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

var timerID = setInterval(function () { alert(left_num1) }, 1000);
like image 200
Quentin Avatar answered Sep 24 '22 13:09

Quentin


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 not passing the function itself.

like image 29
Matt Ball Avatar answered Sep 20 '22 13:09

Matt Ball