读书人

[转]setTimeout 与setInterval 在不同

发布时间: 2012-09-06 10:37:01 作者: rapoo

[转]setTimeout 与setInterval 在不同浏览器上运行的差异
setTimeout和setInterval是延时或定时调用某个函数某段代码。基本上是一样的,接下来就只以setTimeout为例。

通常使用形式:

iTimerID = setTimeout(strJsCode, 500) //strJsCode为一个包含js代码的字符串
iTimerID = setTimeout(objFunction, 500) //objFunction为一个函数对象

看一下下面的代码:

                Functions invoked by setTimeout are passed an extra "lateness" argument in Mozilla,
i.e., the lateness of the timeout in milliseconds. (See bug 10637 and bug 394769.)


3. 其它浏览器(Opera, Safari, Chrome)的setTimeout

和Mozilla系列中的基本一样,但是没有Mozilla系列中的多一个参数的BUG.


解决

1. IE中给setTimeout中的调用函数传参数:

function Integral(name) {    this.name = name;    var derive = function() { alert(this.name); }    // Not applicable    //    setTimeout(derive, 500);    var THIS = this;    // Either of the statements below is ok    setTimeout(derive.apply(THIS), 500);    setTimeout(derive.call(THIS), 500);}new Integral('amount');

读书人网 >Web前端

热点推荐