jQuery .animate() callback infinite loop

前端 未结 5 651
余生分开走
余生分开走 2021-02-11 00:18

A simple question: Why can I do this

var start = function() {
    $(\'#element\').animate({}, 5000, \'linear\', start);
}

but not this

5条回答
  •  余生分开走
    2021-02-11 00:42

    In your second function you are executing the function instead of passing a reference to the function, hence it's going into an infinite loop.

    Change your second function from:

    function start() {
     $('#element').animate({}, 5000, 'linear', start());
    }
    

    to

    function start() {
      $('#element').animate({}, 5000, 'linear', start); //Notice the change to start
    }
    

提交回复
热议问题