Friday, 15 September 2017

javascript - setinterval not working after clearinterval





I have a piece of javascript code:



var intervalId;
intervalId = setInterval(fetchData, 2000);


$('.campaign-select li').on("click", "a", function(event)
{
window.clearInterval(intervalId);
intervalId = setInterval(fetchData_id($(this).data('campaign-id')), 2000);
});


The first setInterval was correct, the function fetchData is correctly loaded each 2 seconds.



But when I click the .campaign-select li a element, the fetchData_id is executed but only once, not repeated as expected with setInterval.




Any help?



Thanks.


Answer



You can't pass parameter in the setInterval function.



That being said, you should try this :



$('.campaign-select li').on("click", "a", function(event){

window.clearInterval(intervalId);
var $this = $(this)
intervalId = setInterval(function(){
fetchData_id($this.data('campaign-id'))
}), 2000);
});

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...