-2

动画(图像更改)不会在 onmouseout 事件上停止。为什么?

JavaScript:

function thumbnail ( type, start, i, m, id, begin, end ) 
{
    if ( type == 1 ) 
    {
        document.getElementById(id).src = begin + id + i + end;

        if ( i == m + 1 ) 
        {
            document.getElementById(id).src = begin + id + end;
            i = 2;
        } 
        else 
        {
            i++;
        }

        setTimeout("thumbnail(1, "+start+", "+i+", "+m+", '"+id+"', '"+begin+"', '"+end+"');", 1000 );
    }

    if ( type == 2 ) 
    {
        document.getElementById(id).src = begin + id + end;
    }
}

HTML:

<img id="aaa" src="http://example.com/file/aaa.png" width="160" height="120" onmouseover="thumbnail(1,2,2,9,'aaa','http://example.com/file/','.png');" onmouseout="thumbnail(2,2,2,9,'aaa','http://example.com/file/','.png');" />

图片:

aaa.png, aaa2.png, ..., aaa9.png

4

2 回答 2

0

因为您setTimeout递归调用并且没有条件可以阻止它。

于 2012-01-25T12:51:22.593 回答
0

尝试这样的事情:

var delay;

function thumbnail ( type, start, i, m, id, begin, end ){
  if ( type == 1 ){
    document.getElementById(id).src = begin + id + i + end;
    if ( i == m + 1 ){
      document.getElementById(id).src = begin + id + end;
      i = 2;
    } 
    else{
      i++;
    }
    if(delay) clearTimeout('delay');
    delay = setTimeout("thumbnail(1, "+start+", "+i+", "+m+", '"+id+"', '"+begin+"', '"+end+"');", 1000 );
  }
  if ( type == 2 ){
    document.getElementById(id).src = begin + id + end;
    clearTimeout('delay');
  }
}
于 2012-01-25T19:43:16.493 回答