Execute script after specific delay using JavaScript

前端 未结 15 2037
陌清茗
陌清茗 2020-11-22 12:29

Is there any JavaScript method similar to the jQuery delay() or wait() (to delay the execution of a script for a specific amount of time)?

15条回答
  •  爱一瞬间的悲伤
    2020-11-22 12:51

    why can't you put the code behind a promise? (typed in off the top of my head)

    new Promise(function(resolve, reject) {
      setTimeout(resolve, 2000);
    }).then(function() {
      console.log('do whatever you wanted to hold off on');
    });

提交回复
热议问题