[英]Load sequencing pictures
I have some pictures I want them to have to organize the page every 10 seconds. 我有一些图片,希望他们每10秒整理一次页面。
As follows: 如下:
, ... ,...
Until we finish all photos. 直到我们完成所有照片。
my problem : 我的问题 :
method load don't work & i don't know method for waiting 方法加载不起作用&我不知道等待方法
Here is my code: 这是我的代码:
<html>
<head>
<title></title>
<script type="text/javascript" src='jq.js'></script>
</head>
<body>
<div></div>
<script type="text/javascript">
var images=["images/0.jpg",
"images/1.jpg",
"images/2.jpg",
"images/3.jpg",
"images/4.jpg",
"images/5.jpg",
"images/6.jpg",
"images/7.jpg",
"images/8.jpg",
"images/9.jpg",
"images/10.jpg"
];
for (var i=0;i<images.length;i++){
var imgtag = '<img src='+images[i]+' />';
$(imgtag).load(function() {
$('div').append(imgtag);
//Wait 10 sec ==> Is there a function to Waiting :-??
}
}
</script>
</body>
</html>
Thanks for your response. 感谢您的答复。
First, you need to wait for the DOM ready event, then use a recursive callback : 首先,您需要等待DOM ready事件,然后使用递归回调:
$(function() { // on DOM ready
var images = [
"images/0.jpg",
"images/1.jpg",
"images/2.jpg",
"images/3.jpg",
"images/4.jpg",
"images/5.jpg",
"images/6.jpg",
"images/7.jpg",
"images/8.jpg",
"images/9.jpg",
"images/10.jpg"
];
(function loadImg(index) {
if (index >= images.length) return; // no more image to load
$('#divId').append( $('<img />').attr('src', srcImg).load(function() {
// invoke this function again in 10 secs incrementing index by 1
setTimeout(function() { loadImg(index + 1); }, 10000);
}) );
})(0); // initiate loading sequence with first image index
});
Use setInterval
function. 使用setInterval
函数。
var images=["images/0.jpg",
"images/1.jpg",
"images/2.jpg",
"images/3.jpg",
"images/4.jpg",
"images/5.jpg",
"images/6.jpg",
"images/7.jpg",
"images/8.jpg",
"images/9.jpg",
"images/10.jpg"
];
var count = 0;
function loadImage() {
if (count < images.length) {
var imgtag = '<img src='+images[count]+' />';
$('div').append(imgtag);
count++;
}
}
setInterval(loadImage, 10000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.