jQuery .delay() doesn't work

后端 未结 8 1029
暗喜
暗喜 2020-12-19 18:26

I\'ve the following JavaScript snippet:

$(\"#dashboard\").addClass(\"standby\").delay(3000).removeClass(\"standby\");
$(\".active\").removeClass(\"active\");         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-19 18:54

    .delay() will only delay animations in jQuery. To set an actual delay, you might want to use setTimeout().

    let cancelId;
    // ...
    cancelId = window.setTimeout(function() {
      // ... do stuff ... 
    }, 3000);
    
    // If you want to cancel prematurely, use `window.clearTimeout`
    if (cancelId) {
      window.clearTimeout(cancelId);
    }
    

    This will execute the code in ... do stuff ... in 3 seconds (3000 miliseconds)

提交回复
热议问题