简体   繁体   English

Javascript图片计时器似乎无法正常运行

[英]Javascript image timer does not seem to be functioning properly

This does not seem to be functioning properly: 这似乎无法正常运行:

var images = [];
images.push({src: "http://www.mountsinaiparks.org/digital-yahrzeit/wp-content/themes/yahrzeit-theme/Candle_images/Candle01.gif", timeout: 3600000});
images.push({src: "http://www.mountsinaiparks.org/digital-yahrzeit/wp-content/themes/yahrzeit-theme/Candle_images/Candle02.gif", timeout: 16560000});
images.push({src: "http://www.mountsinaiparks.org/digital-yahrzeit/wp-content/themes/yahrzeit-theme/Candle_images/Candle03.gif", timeout: 16560000});
images.push({src: "http://www.mountsinaiparks.org/digital-yahrzeit/wp-content/themes/yahrzeit-theme/Candle_images/Candle04.gif", timeout: 16560000});
images.push({src: "http://www.mountsinaiparks.org/digital-yahrzeit/wp-content/themes/yahrzeit-theme/Candle_images/Candle05.gif", timeout: 16560000});
images.push({src: "http://www.mountsinaiparks.org/digital-yahrzeit/wp-content/themes/yahrzeit-theme/Candle_images/candle_end.gif", timeout: 16560000});

//Set the interval with the first element
var x = 0;
var timeout = window.setTimeout(function() {
  changeImage()
}, images[x].timeout);

function changeImage() {
  document.getElementById('candle').src = images[x].src;
  if (x < images.length) {
    x += 1;
  } else {
    x = 0;
  }
  timeout = window.setTimeout(function() {
    changeImage()
  }, images[x].timeout);
}

The timer works okay if the time is in seconds, but I need it to be in hours. 如果时间以秒为单位,计时器可以正常工作,但我需要以小时为单位。 Specifically, one hour for the first image and the rest split over the 24 hour period. 具体来说,第一张图片需要一个小时,其余的则需要24小时。 Could it be that the seconds count is too long? 可能秒数太长了吗? Does it HAVE to be in Milliseconds? 是否必须以毫秒为单位?

setTimeout uses a 32 bit int to store the delay so the maximum value possible is 2147483647. setTimeout使用32位int来存储延迟,因此可能的最大值是2147483647。

So that gives you a maximum of 596.52 hours which is more than enough for what you need. 这样一来,您最多可以使用596.52小时,足以满足您的需求。

But yes, it has to be in milliseconds because that's the only unit setTimeout() accepts. 但是是的,它必须以毫秒为单位,因为这是setTimeout()接受的唯一单位。

Unless you add conversion logic before sending the values to the function. 除非您在将值发送到函数之前添加转换逻辑。

Well the function requires miliseconds, but you can have that conversion logic in your function: 函数需要毫秒,但是您可以在函数中包含转换逻辑:

function changeImage() {
  document.getElementById('candle').src = images[x].src;
       if (x<images.length) {
         x+=1;
       }else{
         x=0;
  }
  var milisecondTimeout = 1000 * 3600 * images[x].timeout;
  timeout = window.setTimeout(function() {changeImage()}, milisecondTimeout);
  }

And then you could push your images with hours instead of miliseconds: 然后,您可以用几个小时而不是毫秒来推送图像:

images.push({src: "http://www.mountsinaiparks.org/digital-yahrzeit/wp-content/themes/yahrzeit-theme/Candle_images/Candle01.gif", timeout: 1});

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM