关于setTimeout不能正确执行

Logging 发布于 2015/08/18 20:08
阅读 655
收藏 0

想让网页上面的一个图片渐隐fadeout 结束之后另外一个图片渐入 fadein

但是在加了settimeout之后,fadeout和 fadein还是同时执行

function fadeout(ele) {
    var stepLength = 1 / 100;
    if (!parseFloat(ele.style.opacity)) {
        ele.style.opacity = 1;
    }
    function step() {
        if (parseFloat(ele.style.opacity) - stepLength > 0) {
            ele.style.opacity = parseFloat(ele.style.opacity) - stepLength;
        } else {
            ele.style.opacity = 0;
            ele.style.display = 'none';
            clearInterval(setIntervalId);
        }
    }
    var setIntervalId = setInterval(step, 5);
}
function fadein(ele)
{
    ele.style.display = 'inline-block';
    var stepLength = 1 / 100;
    if (!parseFloat(ele.style.opacity)) {
        ele.style.opacity = 0;
    }
    function step() {
        if (parseFloat(ele.style.opacity) + stepLength < 1) {
            ele.style.opacity = parseFloat(ele.style.opacity) + stepLength;
        } else {
            ele.style.opacity = 1;
            clearInterval(setIntervalId);
        }
    }
    var setIntervalId = setInterval(step, 5);
}


fadeout(li[0])
setTimeout(fadein(li[1]), 500);//这样调用无法成功

应该怎么修改程序?


加载中
0
Tuesday
Tuesday

不是这样玩的..

setTimeout(function (){ fadein(li[1]); }, 500);

0
终曲
终曲

为啥都不喜欢查文档:

Syntax

var timeoutID = window.setTimeout(func, [delay, param1, param2, ...]);

var timeoutID = window.setTimeout(code, [delay]);

where

  • timeoutID is the numerical ID of the timeout, which can be used later with window.clearTimeout().
  • func is the function you want to execute after delay milliseconds.
  • code in the alternate syntax is a string of code you want to execute after delay milliseconds (using this syntax is not recommended for the same reasons as using eval())
  • delay is the number of milliseconds (thousandths of a second) that the function call should be delayed by. If omitted, it defaults to 0. The actual delay may be longer; see Notes below.
  • param1, param2, and so forth are additional parameters which are passed through to the function specified by func.

所以楼上的可以写成:

setTimeout(fadein, 500, li[1]);

如果函数没参数可以写成:

setTimeout("fadein()", 500);

参数是数字:

setTimeout("fadein(" + param + ")", 500);

参数是字符串:

setTimeout("fadein('" + param + "')", 500);

参数是对象请使用前面直接传递function的调用

返回顶部
顶部