Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.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
Javascript 有没有一种方法可以在这两者之间设置延迟:this.bullets.push(this.ship.shoot());_Javascript - Fatal编程技术网

Javascript 有没有一种方法可以在这两者之间设置延迟:this.bullets.push(this.ship.shoot());

Javascript 有没有一种方法可以在这两者之间设置延迟:this.bullets.push(this.ship.shoot());,javascript,Javascript,是否有办法在两者之间增加延迟; 这个。子弹。推这个。飞船。射击 if (this.mscore >= 1000) { if (input.isPressed("spacebar")) { this.bullets.push(this.ship.shoot()); this.bullets.push(this.ship.shoot()); } } 一种可能的解决方案是使用setTimeout,在下一个示例中,第二个代码将在大约1000毫秒后执行: 您可以使用se

是否有办法在两者之间增加延迟; 这个。子弹。推这个。飞船。射击

if (this.mscore >= 1000) {
  if (input.isPressed("spacebar")) {

    this.bullets.push(this.ship.shoot());
    this.bullets.push(this.ship.shoot());

  }
}

一种可能的解决方案是使用setTimeout,在下一个示例中,第二个代码将在大约1000毫秒后执行:


您可以使用setInterval或setTimeout

下面是setInterval的示例,它将继续拍摄

const pressed=true 设间隔为空 受压{ 射击 间隔时间=设定间隔时间=>射击,1000 }否则{ 间隔=clearIntervalinterval } 功能射击{ 控制台,记录“射击”
}您可以使用setTimeoutk,我将尝试它输入.isPressedspacebar?你确定这是JavaScript吗?如果是这样,请发布定义.isPressed方法的对象或类。那么您将如何对其进行布局,是的,名称以.js结尾的是JavaScript
let delay = 1000;

if (this.mscore >= 1000)
{
    if (input.isPressed("spacebar"))
    {
        this.bullets.push(this.ship.shoot());
        let that = this;

        setTimeout(
            () => that.bullets.push(that.ship.shoot()),
            delay
        );
    }
}