【发布时间】:2016-11-09 00:32:03
【问题描述】:
每次我调用这个 javascript 函数时,我都会遇到一个大问题,它不会等到 left 和 right 图像加载,但它会立即进入下一个操作,我想要的是停止直到left 和 right 图像完美加载图像然后转到syllable_text.html(slice.The_Syl); 行
function showImagesByPairAtInterval(Syllables, interval, index) {
index = index || 0;
let slice = Syllables[index];
left.attr('src', "http://example.com/"+ slice.Url1);
right.attr('src', "http://example.com/"+ slice.Url2);
syllable_text.html(slice.The_Syl);
nextIndex = index + 1;
if (nextIndex <= Syllables.length - 1) {
setTimeout(showImagesByPairAtInterval.bind(
null,
Syllables,
interval,
nextIndex
), interval);
}
}
这里我如何调用这个函数
showImagesByPairAtInterval(syllables.Syllables, 3000);
这里是两个图片
<div id="images" style="display: flex;">
<img src="" id="left_image" name="left_image" alt="" />
<img src="" id="right_image" name="right_image" alt="" />
</div>
更新脚本
使用我在ImagesLoaded 中找到的 ImagesLoaded 脚本后 喜欢
function showImagesByPairAtInterval(Syllables, interval, index) {
index = index || 0;
let slice = Syllables[index];
left.attr('src', "http://deaf-api.azurewebsites.net/"+ slice.Url1);
right.attr('src', "http://deaf-api.azurewebsites.net/"+ slice.Url2);
while (!$("#images").imagesLoaded().always()) {
}
syllable_text.html(slice.The_Syl);
nextIndex = index + 1;
if (nextIndex <= Syllables.length - 1) {
setTimeout(showImagesByPairAtInterval.bind(
null,
Syllables,
interval,
nextIndex
), interval);
}
}
似乎它并不关心所有图像何时真正完成加载,我假设有第二种方法可以使用此脚本
【问题讨论】:
-
我知道它必须是
ready事件,但我不知道如何在这个序列中使用它 -
也许像imagesloaded.desandro.com 这样的东西会有所帮助。否则在这种情况下执行某些代码之前检查两个图像是否已加载有点痛苦。
-
你知道怎么用吗?我会更新上面的问题,告诉你使用
imagesloaded脚本时的错误在哪里
标签: javascript jquery html imagesloaded