var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);var timeoutID = window.setTimeout(code, delay);
var intervalID = window.setInterval(func, delay[, param1, param2, ...]);var intervalID = window.setInterval(code, delay);
<script type="text/javascript">setTimeout( function(param){ alert(param)} , 100, "ok"); </script> 

简单测试了下第五条,在我的电脑上面分别使用firefox与IE9测试,前者可以顺利弹出ok,后者弹出了undefined。
二、“this”问题
由setTimeout()调用的代码运行在与所在函数完全分离的执行环境上. 这会导致,这些代码中包含的 this 关键字会指向 window (全局对象)对象,这和所期望的this的值是不一样的。setInterval的情况类似。
<script type="text/javascript">//this指向windowfunction shape(name) {this.name = name;this.timer = function(){alert("my shape is "+this.name)};setTimeout(this.timer, 50);}new shape("rectangle");</script>
没有被传进去,分别用chrome,firefox和IE9实验了下,都是这个结果。
解决方法一:
<script type="text/javascript">function shape(name) {this.name = name;this.timer = function(){alert("my shape is "+this.name)};var _this = this;setTimeout(function() {_this.timer.call(_this)}, 50);}new shape("rectangle");</script>设置一个局部变量_this,然后放到setTimeout的函数变量中,timer执行call或apply,设置this值。<script type="text/javascript"> //自定义setTimeout与setIntervalvar __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval; window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) { var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2); return __nativeST__(vCallback instanceof Function ? function () {vCallback.apply(oThis, aArgs); } : vCallback, nDelay);}; window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) { var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2); return __nativeSI__(vCallback instanceof Function ? function () {vCallback.apply(oThis, aArgs); } : vCallback, nDelay);}; function shape(name) {this.name = name;this.timer = function(other){alert("my shape is "+this.name);alert("extra param is "+ other);};}var rectangle = new shape("rectangle");setTimeout.call(rectangle, rectangle.timer, 50, "other");</script>1、设置局部变量,赋值为原生的setTimeout与setInterval<script type="text/javascript"> setTimeout(function(){/* Some long block of code... */setTimeout(arguments.callee, 100); }, 10);setInterval(function(){/* Some long block of code... */ }, 100);</script>看上去,两个功能是差不多的,但是里面其实是不一样的。<script type="text/javascript">function sleep(ms) {var start = new Date();while (new Date() - start <= ms) {}}var endTime = null;var i = 0;setInterval(count, 100);function count() {var elapsedTime = endTime ? (new Date() - endTime) : 100;i++;console.log("current count: " + i + "." + "elapsed time: " + elapsedTime + "ms");sleep(200);endTime = new Date();}</script>从firefox的firebug可以查看到,时间间隔很不规则。
setTimeout:
<script type="text/javascript">function sleep(ms) {var start = new Date();while (new Date() - start <= ms) {}}var endTime = null;var i = 0; setTimeout(count, 100);function count() {var elapsedTime = endTime ? (new Date() - endTime) : 100;i++;console.log("current count: " + i + "." + "elapsed time: " + elapsedTime + "ms");sleep(200);endTime = new Date();setTimeout(count, 100);}</script>
以上就是本文的全部内容,希望对大家学习javascript定时器有所帮助。