简体   繁体   English

setInterval() 改变图像

[英]setInterval() change image

The goal: When the page is loaded, display the image andy_black.jpg.目标:页面加载后,显示图片andy_black.jpg。 After two seconds, change the image source, and the thus image in the browser, to a second image called andy_white.jpg.两秒钟后,将图像源以及浏览器中的图像更改为名为 andy_white.jpg 的第二个图像。 This will change back and forth every 2 seconds.这将每 2 秒来回更改一次。

I checked out this article: SetInterval function calls我查看了这篇文章: SetInterval function 调用

(I searched other as well, with the tags [javascript] [function] and the word "setinterval", but most were using jQuery and my intention here is not to use any jQuery, it's an experiment in JavaScript after all). (我也搜索了其他,使用标签 [javascript] [function] 和单词“setinterval”,但大多数都使用 jQuery,我的意图不是使用任何 jQuery,毕竟这是 Z686155AF75A60A0F6E9E9EDD80 中的实验)。

which was quite helpful for before I had read it my code was much longer and the function was not called in the setInterval() function.这在我阅读之前很有帮助,我的代码更长,并且在 setInterval() function 中没有调用 function。

So here's some code: Suggestions?所以这里有一些代码: 建议?

 var i = 1; function change_pic() { i + 1; if (i == 5) { i = 1; } //I suspect the computer will read i as 5 for some //tiny amount of time before reverting back to 1 //which I suspect could cause a further problem, but //is it the source of the current issue? if (i == 1 || i == 2) { document.getElementById('img_to_flip').src = "https://cdns-images.dzcdn.net/images/artist/5d9e44027cc266260d7bd932d98f739d/500x500.jpg"; } else { document.getElementById('img_to_flip').src = "https://media.s-bol.com/q7R3B8QVrAj2/550x549.jpg"; } } var pic_src = setInterval(change_pic, 2000);
 <img id="img_to_flip" src="https://media.s-bol.com/q7R3B8QVrAj2/550x549.jpg" height="100" width="100" />

You forget to actually reassign the new value to i . 您忘记了实际上将新值重新分配给i

Either use: 可以使用:

i = i + 1;

or 要么

++i;

Also, why count to five when you only have two states? 另外,当您只有两个状态时,为什么要计数到五个呢? A common paradigm to have an auto-resetting counter is to use modulo arithmetic: 具有自动重置计数器的常见范例是使用运算:

i = (i + 1) % 2;

which guarantees that i will only ever have values of 0 or 1 . 这保证了i只会拥有01值。

FWIW, here's an alternate way of writing the entire feature that'll work for any number of images - just populate the pics array: FWIW,这是编写可用于任何数量图像的整个功能的另一种方法-只需填充pics数组即可:

(function() {     // function expression closure to contain variables
    var i = 0;
    var pics = [ "andy_white.jpg", "andy_black.jpg" ];
    var el = document.getElementById('img_to_flip');  // el doesn't change
    function toggle() {
        el.src = pics[i];           // set the image
        i = (i + 1) % pics.length;  // update the counter
    }
    setInterval(toggle, 2000);
})();             // invoke the function expression

If you want to avoid the delay in first time setInterval call the function before the setInterval as shown in the top answer:如果您想避免第一次 setInterval 的延迟,请在 setInterval 之前调用 function,如顶部答案所示:

(function() {     // function expression closure to contain variables
    var i = 0;
    var pics = [ "andy_white.jpg", "andy_black.jpg" ];
    var el = document.getElementById('img_to_flip');
    function toggle() {
        el.src = pics[i];           // set the image
        i = (i + 1) % pics.length;  // update the counter
    }
    toggle()
    setInterval(toggle, 2000);
})();             // invoke the function expression

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

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