gpt4 book ai didi

Javascript 循环问题

转载 作者:行者123 更新时间:2023-11-28 20:59:18 25 4
gpt4 key购买 nike

嗨,我的一些 javascript 遇到了一些问题,我正在尝试使用下面的 JS 函数

var changing_thumbs = new Array();
function changeThumb(index, i, thumb_count, path) {
if (changing_thumbs[index]) {
if (path.indexOf('imageCount=') !== -1) {
lastIndexOfEquals = path.lastIndexOf('=');
path = path.substring(0, lastIndexOfEquals + 1);
$j('#' + index).attr('src', path + i);
}
else {
$j('#' + index).attr('src', path + '&imageCount=' + i);
}
i = i % thumb_count + 1;
changing_thumbs[index] = setTimeout("changeThumb('" + index + "', '" + i + "', '" + thumb_count + "', '" + path + "')", 600);
}
}
function startVideoPreview(index, thumb_count, path) {
changing_thumbs[index] = true;
changeThumb(index, 1, thumb_count, path);
}
function endVideoPreview(index, path) {
clearTimeout(changing_thumbs[index]);
document.getElementById(index).src = path;
}

html调用如下

<img id="3a80b9aa-8b2f-4fb9-b3b0-02b2f55bf3be" src="/Image/GetClipImg?photoID=3a80b9aa-8b2f-4fb9-b3b0-02b2f55bf3be&userID=2" alt="Test Clip Description" onmouseout="endVideoPreview('3a80b9aa-8b2f-4fb9-b3b0-02b2f55bf3be', '/Image/GetClipImg?photoID=3a80b9aa-8b2f-4fb9-b3b0-02b2f55bf3be&userID=2')" onmouseover="startVideoPreview('3a80b9aa-8b2f-4fb9-b3b0-02b2f55bf3be', 7, '/Image/GetClipImg?photoID=3a80b9aa-8b2f-4fb9-b3b0-02b2f55bf3be&userID=2')">

一切似乎都工作正常,但这两行

i = i % thumb_count + 1;
changing_thumbs[index] = setTimeout("changeThumb('" + index + "', '" + i + "', '" + thumb_count + "', '" + path + "')", 600);

执行前一个 IF 语句后永远不会被击中,它们只是被踩过。我确信这将是一些基本的东西,但我对 JS 很陌生,我似乎看不出问题是什么。任何提示或技巧将不胜感激。

最佳答案

setTimeout 需要回调。

并且不要使用字符串作为第一个参数,使用函数。

试试这个:

changing_thumbs[index] = setTimeout(function() {
changeThumb(index, i, thumb_count, path);
}, 600);

关于Javascript 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11424426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com