gpt4 book ai didi

javascript - 循环第一次执行和下一次执行之间的延迟

转载 作者:行者123 更新时间:2023-11-29 20:59:38 26 4
gpt4 key购买 nike

我正在尝试制作一个 simon 游戏,我快完成了,但我遇到了一个问题。在 simon 游戏中,当 simon 向您发出您需要记住并随后重复的命令(由闪烁的灯光和每盏灯的音频表示)时,每次闪烁(带有音频)与下一次闪烁之间会有短暂的延迟。

所以现在,我的 simon 正在发出声音,但它是同时发出所有声音,没有延迟。我尝试使用 setIntarvelsetTimeout 但它仍然一次播放所有音频。

由于添加眨眼不应该那么难,所以我将其保留到最后。

然后我构建了一个定时器函数:

function timer(delayInMS) {
var time = new Date();
var currentTime = time.getTime();
var nextTime = time.getTime() + delayInMS;
while (currentTime != nextTime) {
time = new Date();
currentTime = time.getTime();
}
}

并在播放音频的函数中使用它,但它仍在做同样的事情 - 一次播放所有内容。

这是负责音频的功能:

function playAudio(btnSound) {
if (btnSound == "c") {
c.play();
}
if (btnSound == "g") {
g.play();
}
if (btnSound == "a") {
a.play();
}
if (btnSound == "d") {
d.play();
}
}

这个函数负责游戏的逻辑:

var btns = ["c", "g", "a", "d"];
var randomOrder = "";

var playerInputOrder = "";

var timer = 1000; //For timer()

function randomSimon() {
var randomBtn = btns[Math.floor(Math.random() * 4)];
randomOrder += randomBtn;
for (i = 0; i <= randomOrder.length - 1; i++) {
for (s = 0; s <= i; s++) {
var someText = "";
someText += randomOrder.charAt(s);
playAudio(randomOrder.charAt(s));
document.getElementById('debug').innerHTML = someText; //this is for debugin, should be ignored.
timer(500);
}
}

为了更好地阅读,这是整个脚本:

var isGameStarted = false;

var d = new Audio("dSharpNote.wav");
var a = new Audio("aSharpNote.wav");
var g = new Audio("gSharpNote.wav");
var c = new Audio("cNote.wav");

var btns = ["c", "g", "a", "d"];
var randomOrder = "";

var playerInputOrder = "";

var timer = 1000;

function startGame() {
isGameStarted = true; //So players won't be able to just press the simon's buttons.
randomOrder = ""; //A variable to keep the random order given by the simon.
playerInputOrder = ""; //A variable to keep the random order the player inputs.
randomSimon(); //Called to give the first button in the order.
}

function randomSimon() { //Adds one random button to the order and saves the order.
var randomBtn = btns[Math.floor(Math.random() * 4)]; //Adds the random button.
randomOrder += randomBtn; //Saves the random order.
for (i = 0; i <= randomOrder.length - 1; i++) { //this should play all the audios one by one according the saved order
for (s = 0; s <= i; s++) {
var someText = "";
someText += randomOrder.charAt(s);
playAudio(randomOrder.charAt(s));
document.getElementById('debug').innerHTML = someText; //this is for debugin, should be ignored.
timer(500);
}
}
}

function timer(delayInMS) {
var time = new Date();
var currentTime = time.getTime();
var nextTime = time.getTime() + delayInMS;
while (currentTime != nextTime) {
time = new Date();
currentTime = time.getTime();
}
}

function playerProgress(btn) { //Getting the player's input and checks if it's in the right order.
if (isGameStarted == true) {
var btnClicked = btn.id; //Gets the id of the button the player clicked.
playAudio(btnClicked); //So this could play it's audio.
playerInputOrder += btnClicked;
if (playerInputOrder.length == randomOrder.length) {
if (playerInputOrder == randomOrder) {
playerInputOrder = "";
setTimeout(randomSimon, 1000);
} else {
document.getElementById('scoreText').innerHTML = randomOrder + " Game Over!";
isGameStarted = false; //Makes the player unable to play the simon's button's audios.
}
}
document.getElementById('debug').innerHTML = playerInputOrder;
}
}

function playAudio(btnSound) {
if (btnSound == "c") {
c.play();
}
if (btnSound == "g") {
g.play();
}
if (btnSound == "a") {
a.play();
}
if (btnSound == "d") {
d.play();
}
}

function someInterval() {
var i = 0;
var anInterval = setInterval(function() {
i++;
if (i > 11) {
clearInterval(anInterval);
timer *= 2;
}
}, timer);
}

最佳答案

所以你想在每次 playAudio 调用后暂停一下?

我还是会去暂停

var sounds = {};
sounds.a = ...;
sounds.b = ...;

function playAudio(sound, callback) {
if (typeof sounds[sound] === "undefined") {
throw "sound not available";
}
sounds[sound].play();
if (typeof callback !== "undefined") {
window.setTimeout(callback, 500);
}
}

// play a - pause - b - pause c
playAudio("a", function(){
playAudio("b", function() {
playAudio("c");
});
});

如果您先构建序列,您也可以动态执行此操作

var sequence = ["a", "b", "c"];
var pointer = 0;

playAudio(sequene[pointer], function() {
pointer += 1;
if (typeof sequence[pointer] !== "undefined") {
playAudio(sequence[pointer], this); // this will be bound to the function itself
} else {
alert("sequence finished");
}
});

这没有经过测试,但应该可以,如果不行请告诉我。

关于javascript - 循环第一次执行和下一次执行之间的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47267156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com