Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jQuery或普通Javascript中的操作之间暂停_Javascript - Fatal编程技术网

在jQuery或普通Javascript中的操作之间暂停

在jQuery或普通Javascript中的操作之间暂停,javascript,Javascript,我正在做一个游戏,我希望它在每件事情之间有2秒的停顿 因为它不是jQuery包装的选择,所以我不能使用delay()。下面代码的问题是RedFlash()函数发生在暂停之前。也许我需要一个大函数,在数组中运行函数,每个函数之间有2秒的停顿 // Attack Phase function attackPhase() { animateMessage("You slash with your weapon!", "red"); window.setTimeout(function() {

我正在做一个游戏,我希望它在每件事情之间有2秒的停顿

因为它不是jQuery包装的选择,所以我不能使用delay()。下面代码的问题是RedFlash()函数发生在暂停之前。也许我需要一个大函数,在数组中运行函数,每个函数之间有2秒的停顿

// Attack Phase
function attackPhase() {
  animateMessage("You slash with your weapon!", "red");
  window.setTimeout(function() {
     animateMessage("You dealt 15 damage!", "red");
  }, 2000);
  window.setTimeout(function() {
     $('.card_hp').redFlash();
  }, 2000);    
}
抽象地说,是这样的:

// action
// pause 2 seconds
// action
// pause 2 seconds
// action
// pause 2 seconds
// and so on

我找到了几个关于如何暂停一次的答案,但没有找到如何暂停几次并让每个操作等待整整2秒钟的答案。

您的问题是因为您实际上同时启动了两个
setTimeout()
函数

// Attack Phase
  function attackPhase() {
    animateMessage("You slash with your weapon!", "red");
    window.setTimeout(function() {
          animateMessage("You dealt 15 damage!", "red");
          window.setTimeout(function() {
                $('.card_hp').redFlash();
           }, 2000);
    } , 2000);
 }

注意:这不是很好的代码。您应该将设置超时设置为
var
,以便在需要时可以
clearTimeout(var)
。还有很多其他的事情需要考虑,但是超出了这个问题的范围。

您的问题是因为您实际上同时启动了两个
setTimeout()
函数

// Attack Phase
  function attackPhase() {
    animateMessage("You slash with your weapon!", "red");
    window.setTimeout(function() {
          animateMessage("You dealt 15 damage!", "red");
          window.setTimeout(function() {
                $('.card_hp').redFlash();
           }, 2000);
    } , 2000);
 }

注意:这不是很好的代码。您应该将设置超时设置为
var
,以便在需要时可以
clearTimeout(var)
。还有许多其他的事情需要考虑,但超出了这个问题的范围。

下面是一个示例,说明如何将操作放入队列中,并以2秒的间隔一个接一个地执行它们。此外,下次激活某个操作时,该操作将被发布到同一队列,并且在所有之前的操作完成之前不会启动

var queue = [],
    timer,
    processQueue,
    animateMessage,
    attackPhase;

processQueue = function processQueue(force) {
  if(!timer || force) {
    timer = setTimeout(function() {
      if(queue.length > 0) {
        queue.splice(0,1)[0]();
        processQueue(true);
      } else {
        timer = null;
      }
    }, 2000);
  }
};

animateMessage = function animateMessage(msg, color) {
  console.log(msg);
};

attackPhase = function attackPhase() {
  queue.push(function(){
    animateMessage("You slash with your weapon!", "red");
  });
  queue.push(function() {
    animateMessage("You dealt 15 damage!", "red");
  });
  processQueue();
};

attackPhase();
attackPhase();

这里是一个工作示例

这里是一个如何将操作放入队列并以2秒的间隔逐个执行的示例。此外,下次激活某个操作时,该操作将被发布到同一队列,并且在所有之前的操作完成之前不会启动

var queue = [],
    timer,
    processQueue,
    animateMessage,
    attackPhase;

processQueue = function processQueue(force) {
  if(!timer || force) {
    timer = setTimeout(function() {
      if(queue.length > 0) {
        queue.splice(0,1)[0]();
        processQueue(true);
      } else {
        timer = null;
      }
    }, 2000);
  }
};

animateMessage = function animateMessage(msg, color) {
  console.log(msg);
};

attackPhase = function attackPhase() {
  queue.push(function(){
    animateMessage("You slash with your weapon!", "red");
  });
  queue.push(function() {
    animateMessage("You dealt 15 damage!", "red");
  });
  processQueue();
};

attackPhase();
attackPhase();

下面是一个工作示例

您可能还想禁用某些输入/命令,使其在暂停状态下发生,因为这可能会触发一系列动画和等待。哇,超快速回复!好的,是的,我真的应该清理这个代码。谢谢你的指导。我看到你把它们放在一起了。明白了。你可能还想在暂停状态下禁用某些输入/命令,因为这可能会触发一系列动画和等待。哇,超快速回复!好的,是的,我真的应该清理这个代码。谢谢你的指导。我看到你把它们放在一起了。知道了。