循环的Javascript问题

时间:2012-07-11 01:46:32

标签: javascript jquery

嗨我有一些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很新,我似乎无法看到问题所在。任何提示或提示将不胜感激。

1 个答案:

答案 0 :(得分:3)

setTimeout需要回电。

不要使用string作为第一个参数,使用函数。

试试这个:

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