简体   繁体   English

纯javascript中自动幻灯片放映的问题

[英]Issues with Automatic Slideshow in pure javascript

I am trying to display 4 pictures in an automatic slideshow by building it in pure javascript. 我试图通过使用纯JavaScript构建自动幻灯片显示4张图片。 setInterval() and setTimeout() functions are really confusing me. setInterval()和setTimeout()函数确实让我感到困惑。

The code displays first pic and then waits for 3sec and displays the last pic and then ends.....duno why?? 该代码显示第一个图片,然后等待3秒,显示最后一个图片,然后结束..... duno为什么?

var divImages = document.getElementById("images");
var img1 = document.getElementById("img1"); //display:none using CSS
var img2 = document.getElementById("img2"); //display:none using CSS
var img3 = document.getElementById("img3"); //display:none using CSS
var img4 = document.getElementById("img4"); //display:none using CSS

window.addEventListener("load", getFirstImage);


function getFirstImage(){
  img1.style.display = "block";
}

function getSecondImage(){
  img2.style.display = "block";
  img1.style.display = "none";
}
setTimeout(getSecondImage,3000);

function getThirdImage(){
  img3.style.display = "block";
  img2.style.display = "none";
}
setTimeout(getThirdImage,3000);

function getFourthImage(){
  img4.style.display = "block";
  img3.style.display = "none";
}
setTimeout(getFourthImage,3000);

Here's a working solution. 这是一个可行的解决方案。 Hope it helps! 希望能帮助到你!

 setInterval(miFuncion, 2500); var counter = 1; function miFuncion() { var elemento = document.getElementById("foto"); if(counter === 0 ){ elemento.src = "http://i2.cdn.cnn.com/cnnnext/dam/assets/141027105451-pkg-petroff-ferrari-sergio-elite-car-00014417-story-top.jpg"; } else if(counter === 1){ elemento.src = "http://www.fashiontrends.pk/wp-content/uploads/2012/02/Porsche-Sports-Car.jpg"; } else if(counter === 2){ elemento.src = "http://bestcarlive.com/wp-content/uploads/bmw-sports-car-wallpapers-20140720034912-53cb3c380a9cf.jpg"; } else if(counter === 3){ elemento.src = "http://media.caranddriver.com/images/17q1/674191/2018-kia-stinger-sports-sedan-photos-and-info-news-car-and-driver-photo-674389-s-450x274.jpg"; counter = -1; } ++counter; } 
 <img src="http://i2.cdn.cnn.com/cnnnext/dam/assets/141027105451-pkg-petroff-ferrari-sergio-elite-car-00014417-story-top.jpg" id="foto" width="100%" height="350"> 

It's very easy but before I explain try this 这很容易,但是在我解释之前请尝试一下

setTimeout("alert('a')",3000)
setTimeout("alert('b')",3000)

You see? 你看? Both run exactly after 3 seconds why? 两者都恰好在3秒后运行,为什么? You ask because 你问是因为

setTimeouts are asynchronous setTimeouts是异步的

ie. 即。 they won't wait for one another .When javascript interpreter sees the first setTimeout it records that in it's “stuff to do after 3 secs” now after recording that it read the second setTimeout and this also gets recorded in the same list but real point to note is that almost zero time has passed between the last two events moreover due to incredible speed of js engine even if you put a huge code( even k lines of code) in between the two setTimeouts they still would run after 3 secs in order of their appearance.That's why all 4 slide functions ran after 3 secs in order of their setTimeouts since getFourthImage() was the last to run only its effect remained 他们不会再等了 。当javascript解释器看到第一个setTimeout时,它在记录下来读取第二个setTimeout之后将其记录在“ 3秒钟后要做的事情”中,并且它也记录在同一列表中,但实际是需要注意的是,由于js引擎的惊人速度,即使在最后两个事件之间经过了几乎零时间,即使您在两个setTimeouts之间放置了巨大的代码(甚至k行代码),它们仍然可以按顺序运行3秒这就是为什么所有4个幻灯片功能都按照其setTimeouts的顺序在3秒后运行的原因,因为getFourthImage()是最后一次运行,只保留了其效果

so how do you cascade them ? 那么您如何级联它们呢? Just put settimeout of a slide in it's preceding slides function sort of like alarm or like some relay race 只需将幻灯片的settimeout放到它前面的幻灯片中,就可以像警报或一些接力赛

window.addEventListener("load", getFirstImage);


function getFirstImage(){
  img1.style.display = "block";
  setTimeout(getSecondImage,3000);
}

function getSecondImage(){
  img2.style.display = "block";
  img1.style.display = "none";
  setTimeout(getThirdImage,3000);
}


function getThirdImage(){
  img3.style.display = "block";
  img2.style.display = "none";
  setTimeout(getFourthImage,3000);
}

function getFourthImage(){
  img4.style.display = "block";
  img3.style.display = "none";   // last one no need to put anything but you can if you wish
}
 <script type="text/javascript">
 $("#slideshow > div:gt(0)").hide();
 setInterval(function() {
 $('#slideshow > div:first')
    .fadeOut(1000)
    .next()
    .fadeIn(1000)
    .end()
    .appendTo('#slideshow');
}, 1200);
</script>

I see that you have getElementById, so I'm assuming you have some HTML? 我看到您有getElementById,所以我假设您有一些HTML? this is a jquery script with assumption you have a div with a class named slideshow any div you nest in that div will be in the slideshow! 这是一个jquery脚本,假设您有一个名为slideshow的类的div,您嵌套在该div中的任何div都将在幻灯片中! hope this helps 希望这可以帮助

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

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