С вашим кодом много проблем.
var round = prompt("enter text"); // how do you know the user enters a number?
var defaultNumberOfRounds = 1; // I added this row
// CHANGED THIS IN EDIT
var roundno = round; // should be: isNaN(Number(round)) ? defaultNumberOfRounds : round
var images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];
var size = images_arr.length
function myFunction() {
// CHANGED THIS IN EDIT the conditions within () should be: var i = 0; i < roundno; i++
for (var i = 0; i = roundno; i + 1) {
console.log(i);
// all iterations in the loop will execute this at the same time.
setInterval(function () {
var x = Math.floor(size * Math.random())
$('#random').attr('src', images_arr[x]); // JQuery
}, 1500);
// all iterations in the loop will execute this at the same time.
setInterval(function () {
var sound = new Audio("../audio/audio.mp3");
sound.play();
}, 3000);
if (i = roundno) { // should be i == roundno
break; // don't need to break it, because your for loop's condition should take care of this
}
}
}
} // ADDED THIS IN EDIT: missing curly bracket
[edit] Я добавил фрагмент, чтобы показать, что мой код работает. Я прокомментировал весь код для l oop, и мне пришлось изменить объявление roundno
и выражение внутри for l oop.
var round = prompt("enter text");
var defaultNumberOfRounds = 1;
var roundno = isNaN(Number(round)) ? defaultNumberOfRounds : round;
var images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];
var size = images_arr.length;
console.log(`round: ${round}, roundno: ${roundno}`);
function myFunction() {
for (var i = 0; i < roundno; i++) {
console.log(i);
/*setInterval(function () {
var x = Math.floor(size * Math.random())
$('#random').attr('src', images_arr[x]); // JQuery
}, 1500);
setInterval(function () {
var sound = new Audio("../audio/audio.mp3");
sound.play();
}, 3000);*/
}
}
myFunction();