Jquery setInterval() not working

前端 未结 5 2065
野性不改
野性不改 2020-12-20 16:30

I\'m trying to create a kind of slideshow.

The problem:

function slides(x) {
      $(\"#irack\").stop().animate({\"left\": x}, 20);
 };
setInterval         


        
5条回答
  •  清歌不尽
    2020-12-20 16:59

    The reason it doesn't work is because you're calling the function you're passing to setInterval

    You'll need to wrap the function in an anonymous function to pass a parameter as part of the interval.

    function slides(x) {
          $("#irack").stop().animate({"left": x}, 20);
     };
    setInterval(function(){slides(-30)},300);
    

    Note how the slides(-30) is now wrapped in an anon function.

提交回复
热议问题