wait() or sleep() function in jquery?

前端 未结 7 711
醉酒成梦
醉酒成梦 2020-12-04 20:48

I want to add a class, wait 2 seconds and add another class.

.addClass(\"load\").wait(2sec).addClass(\"done\");

Is there any way?

7条回答
  •  遥遥无期
    2020-12-04 21:02

    delay() will not do the job. The problem with delay() is it's part of the animation system, and only applies to animation queues.

    What if you want to wait before executing something outside of animation??

    Use this:

    window.setTimeout(function(){
                     // do whatever you want to do     
                      }, 600);
    

    What happens?: In this scenario it waits 600 miliseconds before executing the code specified within the curly braces.

    This helped me a great deal once I figured it out and hope it will help you as well!

    IMPORTANT NOTICE: 'window.setTimeout' happens asynchronously. Keep that in mind when writing your code!

提交回复
热议问题