wait() or sleep() function in jquery?

前端 未结 7 726
醉酒成梦
醉酒成梦 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:25

    setTimeout will execute some code after a delay of some period of time (measured in milliseconds). However, an important note: because of the nature of javascript, the rest of the code continues to run after the timer is setup:

    $('#someid').addClass("load");
    
    setTimeout(function(){
      $('#someid').addClass("done");
    }, 2000);
    
    // Any code here will execute immediately after the 'load' class is added to the element.
    

提交回复
热议问题