循环以检查数组中的最后一个图像
Loop to check last image in array
感谢一位好心的成员,我找到了解决我的代码问题的方法!但我又在绕圈子了。。我试图在回答的问题中找到解决方案,但什么都不起作用。
我想要一个循环来检查是否加载了数组中的最后一个图像。我正在做一个简单的游戏,你点击一张图片,它就会变成下一张图片。目标是你必须在图像上点击多次,才能到达最后一张图像。如果你在最后一张照片上,你就赢了。如果你在最后一张照片上,需要有一个计时器在5秒后进行检查。(我有多张图片,你必须点击,我现在只显示一张)
所以我做了一个这样的for循环:
for (eiImg == 'img/ei4.png'){
alert("yay!");
}
这可能是非常非常错误的,但我是一个非常业余的程序员,所以我很抱歉!)当然没用。我甚至没有用定时器。。
有人能教我如何成功地进行循环,在5秒钟后检查图像是否是阵列中的最后一个图像吗。我试过用谷歌搜索,但就是找不到解决方案。
这是我的全部javascript代码:
var eieren = 0;
var eieren = Math.floor((Math.random() * 4) + 1);
var imgArray = [ 'img/ei1.png' , 'img/ei2.png' , 'img/ei3.png', 'img/ei4.png' ];
imgArray.length;
var eiImg = imgArray[eieren - 1];
console.log( eiImg );
// thanks so much for the help Azzy Elvul!
document.getElementsByTagName( 'img' )[0].src = eiImg;
document.getElementsByTagName( 'img' )[0].addEventListener( "click", changeImage ());
var counter = eieren - 1;
function changeImage()
{
//Load new image if this is not the last image
if ( counter < imgArray.length - 1 )
{
document.getElementsByTagName( 'img' )[0].src = imgArray[ ++counter % imgArray.length];
}
};
检查此项,它会创建数组中最后一个对象的警报
var imgArray = [ 'img/ei1.png' , 'img/ei2.png' , 'img/ei3.png', 'img/ei4.png' ];
var alertIndex = imgArray.length-1; // the last index from the array
for(var i = 0; i<imgArray.length; i++) {
if(i == alertIndex) {
alert(imgArray[i]);
}
}
JSFiddle-看看这个工作示例。
好吧,所以我对这个和你正在开发的解决方案很好奇,所以我自己尝试了一下,看看是否能让它发挥作用。我认为我的解决方案与您的方法略有不同。下面是带注释的代码,最后是代码的JSFiddle演示。
var imgArray = ['img/ei1.png', 'img/ei2.png', 'img/ei3.png', 'img/ei4.png'];
// get the body element and create an array to keep the image elements
var body = document.querySelector('body');
var imgs = [];
// loop over the list of images, creating new elements
// adding src attributes and click events to them, and then adding
// them to the imgs array we created
for (var i = 0, l = imgArray.length; i < l; i++) {
var img = document.createElement('img');
img.src = imgArray[i];
img.addEventListener('click', changeImage);
imgs.push(img);
}
function changeImage() {
// are there any image elements left in the img array
// if there are, continue
if (imgs.length > 0) {
// is the length of the img array less than the length
// of the imgArray - if so, remove the previous image from the page
if (imgArray.length !== imgs.length) {
body.removeChild(document.querySelector('img'));
}
// random number based on the number of image elements
// remaining in the imgs array
var random = Math.floor((Math.random() * imgs.length - 1) + 1);
// add that image to the body element
body.appendChild(imgs[random]);
// remove the image element from the imgs array
imgs.splice(random, 1);
}
}
function checkOnLastImage() {
// are you on the last image?
if (imgs.length === 0) {
console.log('congratulations')
} else {
console.log('try again');
}
}
// run changeImage for the first time
changeImage();
// use a setTimeout to check at 5 seconds whether
// the last image has been reached
setTimeout(checkOnLastImage, 5000);
演示
为什么要迭代?直接获取最后一个对象。
alert(arr[arr.length-1])
相关文章:
- 循环以检查数组中的最后一个图像
- 使用 jQuery 在最后一个图像之后附加 HTML
- 多个图像显示带有动态创建的画布的最后一个图像并分配给图像
- 使jQuery图像滑块停止在最后一个图像,并添加下一个和上一个按钮
- 最后一个图像已完成淡入
- ajax 返回带有循环的图像长度,检查最后一个循环
- 使用onload加载多个图像以调用函数,并且只有最后一个加载的图像调用该函数
- 为什么我的脚本只将最后一个图像上传到我的 FTP 和数据库
- 最后一个图库图像上的fancybox禁用按钮
- 为什么jQuery代码将最后一个图像替换为第一个
- Fancybox重复第一个和最后一个图像
- 使连续的图像只出现在最后一个消失后,而悬停
- Javascript幻灯片显示最后一个图像5次
- 我怎样才能使一个图像滑块停止在最后一个图像和重放按钮
- 使用javascript将图像更改为其他图像,并从最后一个图像重复到第一个图像
- 数组中的最后一个图像重复两次,为什么?
- 图像加载仅加载最后一个图像
- 从PHP中删除最后一个逗号,该逗号将进入javascript图像预加载脚本
- JQuery滑块:从最后一个图像转换到第一个图像
- 在重置循环(从头开始滑块)之前暂停滑块中的最后一个图像