jQuery .animate() callback infinite loop

前端 未结 5 877
我在风中等你
我在风中等你 2021-02-10 23:54

A simple question: Why can I do this

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

but not this

5条回答
  •  醉酒成梦
    2021-02-11 00:38

    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
    }
    

提交回复
热议问题