单击1按钮停止旋转3个图像

时间:2016-03-21 10:44:28

标签: javascript

我一直试图找出如何在不同时间停止3张图像,同时按下水果机的手柄就可以了,我可以用JavaScript做到这一点。

const images = ["plum.jpg", "cherries.jpg", "pineapple.jpg", "lemon.jpg", "apple.jpg", "bananas.jpg"];

var count;
const speed = 100;

function beginShow(){
    count = 20;
   nextImage();}

function nextImage(){
changeImage("pic1");

count = count - 1;  // count down
if (count > 0)      // repeat unless we're at zero
setTimeout(function(){alert("Try Again") }, 1000); // pop up after 1 second saying try again 
changeImage("pic2");

count = count - 1;  // count down
if (count > 0)      // repeat unless we're at zero
{
    tim = setTimeout("nextImage();", 200);
}

changeImage("pic3");

count = count - 1;  // count down
if (count > 0)      // repeat unless we're at zero

{
    tim = setTimeout("nextImage();", 300);
}}

function changeImage(imageId){
var rand = Math.floor(Math.random() * images.length);
document.getElementById(imageId).src =images[rand];
// changes handle from not pulled to pulled
var image = document.getElementById('Lever');
if (image.src.match("Lever2")) {
    image.src = "lever1.jpg";
} else {
    image.src = "lever2.jpg"; }}    

1 个答案:

答案 0 :(得分:0)

由于这似乎是学校练习,我不会发布代码。

但是,请注意:count是全局变量,并且是在封闭范围(函数)中访问的。或者在运行时创建计数,并设置其默认值(0)...

另外,你的setTimeout似乎是错误的......

如果您打算在x时间内执行某项操作,然后x+1,然后x+2,则只需连接超时(并请read some documentation on MDN site

编辑:

可以提供帮助的示例如下:

function roll(){
   var timer1 = window.setTimeout(function(){document.getElementById("pic1").src=images[Math.floor(Math.random() * images.length)];}, 1000);
   var timer2 = window.setTimeout(function(){document.getElementById("pic2").src=images[Math.floor(Math.random() * images.length)];}, 2000);
   var timer3 = window.setTimeout(function(){document.getElementById("pic3").src=images[Math.floor(Math.random() * images.length)];}, 3000);
}