一、解释说明

1、概述

setTimeout:在指定的延迟时间之后调用一个函数或者执行一个代码片段

setInterval:周期性地调用一个函数(function)或者执行一段代码。

2、语法

setTimeout:

var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
var timeoutID = window.setTimeout(code, delay);
  1. timeoutID 是该延时操作的数字ID, 此ID随后可以用来作为window.clearTimeout方法的参数
  2. func 是你想要在delay毫秒之后执行的函数
  3. code 在第二种语法,是指你想要在delay毫秒之后执行的代码
  4. delay 是延迟的毫秒数 (一秒等于1000毫秒),函数的调用会在该延迟之后发生.但是实际的延迟时间可能会稍长一点
  5. 标准浏览器与IE10支持第一种语法中向延迟函数传递额外参数的功能

setInterval

var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
var intervalID = window.setInterval(code, delay);
  1. intervalID 是此重复操作的唯一辨识符,可以作为参数传给clearInterval()。
  2. func 是你想要重复调用的函数。
  3. code 是另一种语法的应用,是指你想要重复执行的一段字符串构成的代码
  4. delay 是每次延迟的毫秒数 (一秒等于1000毫秒),函数的每次调用会在该延迟之后发生。和setTimeout一样,实际的延迟时间可能会稍长一点。
  5. 标准浏览器与IE10支持第一种语法中向延迟函数传递额外参数的功能

  

<script type="text/javascript">
setTimeout( function(param){ alert(param)} , 100, 'ok');
</script>

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAACsCAIAAAAbueUQAAAIYklEQVR4nO3cz27bVhrG4XNHvgE1Sbtwsy0CdFPoAtibEGB7I0cief58i5mxEfQOrF1TNGhRN6vWjtMobpEi2wLpzGDqpF24WWQWlFlGViyLPQTFc34PDIGW/dIHhr+IFPlGiUiuba6tiDiRLDfaOBGxTtJMG+tExFiXZto6ERFtXJYb54QgwQCCatHX7PKdysKdEiTYsaDKci0iIpLlevYdTtIsL0bEWBmnmbGz3aVZXs4TQYIBBFWurRNxIrm25VhkudGzeZI017OdWpfl5mK2LEGCAQSViGS5yXJTjEia5eWIjMaz1xJt3Wic2Yt5SrP8Yp4IEux2UDkn2jht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WSqUCgYDL5dbW1jIYDCqVWlZWRiaTi4qKCgoK8vLycnJycnJysrOzb968mY1nuQSs1ry8vIKCgqKiIjKZXFZWRqVSGQxGbW0tl8sVCARSqbS7uxtsec1mMxj2Is1vrv48Hg+6BQ4MDIDDwQjBtrY2kUjE4/HYbHZtbW11dXVVVVVlZWVFRUVZWVlJSQmFQiHjWV6hUCglJSVlZWUVFRWVlZVVVVXV1dW1tbVsNpvH44lEIjQ+cMBtYGAA3fzAxnc2f9NbIJgLBIdDAEGlUimXyzs6OlpbW4VCIY/H43A49+7dq62trampYTKZDAaDwWDcwbO8AlYrk8msqampra29d+8eh8Ph8XhCobC1tbWjo0MulyuVSoDParU6nU632z08PDx78wvsD7TAKQRtNpvJZNLpdGq1ure3VyaTdXR0SKVSsVgsFAr5fH5TU1NjYyOHw2Gz2Q14llfYbDaHw2lsbGxqauLz+UKhUCwWS6XSjo4OmUzW29urVqt1Op3JZLLZbNPxzdT8pvqbhSDYEJvNZoPBoNVqVSpVb2+vXC7v6uoCEFtbWyUSiVgsFolEIpFIKBQKhUIRnuUSsVgskUhaW1sBu66uLrlc3tvbq1KptFqtwWAwm81gszt3fDP6m04Q7As6HA6r1QoU9vf3azQaALGnp0cul8tksu7ubvB5Op2dnZ2dnQv0YT14Fj/d3d0ymUwul/f09AB2Go2mv78fyLNarQ6HA+zzBcQ3V38zERwZGRkaGgKN0OFw2Gw2s9lsNBr1ej2AqFar1Wq10p++vr6+vj4lnmUUsIoBO71ebzQazWazzWYD8txu99DQ0MjIyNzxBfYXkODY2BjSCF0ul9PpdDgcdrvdarVaLBaTyWQymYxGo8Fg0ONZjjEYDEajEaxoi8VitVrtdrvD4XA6nS6XC2l7Y2Njc8fn9Xr/PwHAs1pr/q7aAAAAAElFTkSuQmCC" alt="" />  

简单测试了下第五条,在我的电脑上面分别使用firefox与IE9测试,前者可以顺利弹出ok,后者弹出了undefined。

二、“this”问题

由setTimeout()调用的代码运行在与所在函数完全分离的执行环境上. 这会导致,这些代码中包含的 this 关键字会指向 window (全局对象)对象,这和所期望的this的值是不一样的。setInterval的情况类似。

<script type="text/javascript">
//this指向window
function shape(name) {
this.name = name;
this.timer = function(){alert('my shape is '+this.name)};
setTimeout(this.timer, 50);
}
new shape('rectangle');
</script>

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARYAAACoCAIAAACqmFapAAAI4ElEQVR4nO3cT2/bRhrH8XlHegNqkvbg5loE6KXQC2DfhADbF7kS/8yfQ3dtBH0H0q0pGrSom1PrxGkVt8gi1wXS7mLrpD24OXgPIzMMRdmMZuiJx98PDGFM8RE1Bh+QovizMMbsjCd5oYwxSpvRzmdSGWNMLtXOeGKMMcakeTFOMzuepFmaF3ZMIYUUCqmM/SmUvmis5dm4xcoUUngtCkVeSGOM1mZnPCmUNsYUSu98NtHaGGPyQo0ni9ZMsyLNFq05nmQUUkjheJIJqUwhtVRaayOVyaWSymhtpNKF1FIZpU2hdCG10kZpU0hdKK20oZBCCpU2wnZbIZUxRiqzM55IZYwxhdRla2a5TLP8rE3zLJdlm1JI4TUvFIXU9um8UPbp6riQOi+U0kbb1pRaa6O0yQtFIYUU5oUS9mqD1mY8ycqzvfEkU2dne5Oz1sxyWbZmmuUUUkjhJMuF0ibLpX1aKpPmxaLDlM5yaVctpMoLpbXRxrbm4hofhRRSKLQ2aZaXB6xxmpUHqTTLyysP2dll8iwvytakkEIKF5+FlDZpVshFt+l0cdQyhdRZLsttLF7XmCyXFFJIYZZLUR6wznt1U3/1z//xz+3t7QS44ra3t3d39xp38iyXbbqjfiI3aXeM29raOjw8PAWuuIcPH25ubjbu5GbpRK6xO97mcoJ5/UkrSZLQcwf8SJKkcSc3LS8nXHjZLm263kcLIRpJkjTu5G0uaqdZXv1qVVa+PJIrvnVS9lsnWgjRSJKkcSfP3/hqdWV3rHmDDy2EaNgTufVv8JHKFFK9vpGuqN5Itxgv34FHCyEaSZI07uR6cZvpBd2xIuwwvuA+cFoI0UiS5O3CDuOlsIOsBImUfj2W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" />

没有被传进去,分别用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值。

function能够调用局部变量_this,多亏了Javascript的闭包。里面涉及了作用域链等知识,有兴趣的可以自己去了解下,这里不展开了。

解决方法二:

这个方法有点高大上。自定义了setTimeout与setInterval。而且还扩展了低版本的IE浏览器,不支持向延迟函数传递额外参数的问题。

<script type="text/javascript">
  //自定义setTimeout与setInterval
var __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

2、扩展setTimeout与setInterval,aArgs通过分割arguments这个变量,获取到额外的参数数组

3、用vCallback instanceof Function判断这是不是一个函数或代码,如果是函数就用apply执行

4、setTimeout用call执行,设定this对象,以及其它的func、delay等参数

5、顺便扩展setTimeout,IE低版本的浏览器也能执行额外参数

三、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>

粗看上去,两个功能是差不多的,但是里面其实是不一样的。

setTimeout回调函数的执行和上一次执行之间的间隔至少有100ms(可能会更多,但不会少于100ms)

setInterval的回调函数将尝试每隔100ms执行一次,不论上次是否执行完毕,时间间隔理论上是会<=delay的。

setInterval:

<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可以查看到,时间间隔很不规则。

情况大致是这样的:由于count函数的执行时间远大于setInterval的定时间隔,那么定时触发线程就会源源不断的产生异步定时事件,并放到任务队列尾而不管它们是否已被处理,但一旦一个定时事件任务处理完,这些排列中的剩余定时事件就依次不间断的被执行。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAACpCAIAAACK3/xKAAAJU0lEQVR4nO3du5HDRhaF4c4CYcBAEIwCDnJgEHAQAmOAg7XWoysTVao25agQgdyW0Q80XnyJBHjZ/1c0VjNDvGZ52IRG56q//v6HBw8ePEQ81P/+/wcPHjx4iHgoAwBCEFgAxCCwAIhBYAEQg8ACIAaBBUAMAutFur10/XD0UQBp2T+whrq+fN8unnqKLpRSKnt7YOm2UarQm9+dR2RTFlXdvfUQJtehqyulVHRI9sTVu3cKPGrvwBr67lQ2X7UL3TZKqeLJo+rq8ydWWE1ZbgVWU54+uqabXYeh7/K8GqLr2ZRF0+odjgTYMgks/46adf0w9F2msq4fwtt+UxZKqaZtC6WUKq59lymV5dWlrsK77nILZXlSSmV5NfiXhLq5PIm3YIwZ+i6z7/L+NVOUjTFDlWdV3T24i66u7He36fJWYLmVRRxqcWCtHWRpn2Jf4eEHwmpl6zQ3VlhDlbvv+2OwX3FPD2ed5/nq7+KF69DVlT14Y4ZzeR6MPvnn6rZp2qs/pCzPM3vY984C+K/GwApvpEPfnd3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8fKf7rIUZ+AZ0ujs3G+8S7XSPT5NOd3wDOt2dUAq6lHKn+41d0OmO/dHpHm98/ZsJd7rfDiw63bE3Ot3jja9LtdP9/i7odMfO6HR3PxA+2NLpvtxF02o63fEN+Dus96PTHfgQAguAGAQWADEILABiEFgAxCCwAIjBEAoAYrDCAiAGgQVADAILgBgEFgAxCCwAYhBYAMQgsF5E2TmwP4ZQLGtV7j8jjSEUy15m4GAMoVh2BD4kgSEUrzRNAx/FEIoxp2bN65E0h1Doc1lmfgsMocA3YAiFCXm09apLdgjFtW2H8ZfCEAocjyEUcSaufspLegiFvQLn8jwwhAJfgCEUbk6E2b6HleYQinCFt1ZYDKHA/hhCMa53Ch+FDKGY7qLQ7rsMocDB+Dus92MIBfAhBBYAMQgsAGIQWADEILAAiEGnOwAxWGEBEIPAAiAGgQVAjH8BGZfypl+z89cAAAAASUVORK5CYII=" alt="" />

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>

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAO0AAACkCAIAAAD0eD58AAAHhUlEQVR4nO2dvZGsOBRGlQVhYBBER4FDDhMEDiF0DDistd64z6RqiwzIQmsIhEBAN7z+EVfn1Bi704B++LitecUpqX/+/cMPP1f/URrg+pBjkAA5BgmQY5AAOQYJkGOQADk+Q1ffm7b/di9g4sM57svyHl4Th07pMqWUSl6e466ulMq6zU+XT06VZ0XZvLQLs3loykIp5XTJDFy9utHX8NEc921zy6ugmujqSimVHexVU/68ox5Xeb6V4yq/vfUbYDEPfdukadE781nlWVV3H+jJOaYcj89f0rR93zaJSpq2t0WiyjOlVFXXmVJKZb9tkyiVpMW9LOwz6l8hz29KqSQ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NQzZR/IiSt4ptPtYR9I9oG8sGdqSxf7QG50kn0gowTP9CuQY5AAOQYJkGOQADkGCfwPmasu+Ag8xLcAAAAASUVORK5CYII=" alt="" />

demo下载:

http://download.csdn.net/download/loneleaf1/7961057

参考资料:

http://ejohn.org/blog/how-javascript-timers-work/  How JavaScript Timers Work

https://developer.mozilla.org/zh-CN/docs/DOM/window.setTimeout window.setTimeout

https://developer.mozilla.org/zh-CN/docs/Web/API/Window.setInterval Window.setInterval

http://heroicyang.com/2012/09/06/javascript-timers/ 理解JavaScript定时器:setTimeout和setInterval

Javascript定时器(二)——setTimeout与setInterval的更多相关文章

  1. JavaScript定时器:setTimeout()和setInterval()

    1 超时调用setTimeout() 顾名思义,超时调用的意思就是在一段实际之后调用(在执行代码之前要等待多少毫秒) setTimeout()他可以接收两个参数: 1 要执行的代码或函数 2 毫秒(在 ...

  2. javascript 函数返回值(return)、定时器(setTimeout、setInterval)

    一.函数的返回值:return 1.函数名+括号=return后面的值 <script> function fn1(){ return 100; } alert(fn1()); // 10 ...

  3. Javascript定时器(三)——setTimeout(func, 0)

    setTimeout(func, 0)可以使用在很多地方,拆分循环.模拟事件捕获.页面渲染等 一.setTimeout中的delay参数为0,并不是指马上执行 <script type=&quo ...

  4. JavaScript定时机制setTimeout与setInterval研究

    JavaScript的setTimeout与setInterval是两个很容易欺骗别人感情的方法,因为我们开始常常以为调用了就会按既定的方式执行, 我想不少人都深有同感, 例如 setTimeout( ...

  5. Javascript 定时器的使用陷阱 (setInterval)

    setTimeout(function(){ // 其他代码 setTimeout(arguments.callee, interval); }, interval); setInterval会产生回 ...

  6. js定时器window.setTimeout和setInterval

    window.setTimeout(function(){                            document.getElementById("editorindex&q ...

  7. JavaScript中的setTimeout和setInterval

    上一篇博文<浏览器中Javascript单线程分析>中描述了浏览器中Javascript单线程的原理. 在此基础上,这篇文章将主要介绍setTimeout/setInterval是如何模拟 ...

  8. js中两种定时器,setTimeout和setInterval的区别

    setTimeout只在指定时间后执行一次,代码如下:   <script>   //定时器 异步运行   function hello(){   alert("hello&qu ...

  9. JavaScript中的setTimeout、setInterval和随机函数制作简易抽奖小程序

    几乎所有计算机语言有都内置随机函数.当然这种随机,人们习惯称为伪随机数发生器,产生的是一个[0,1)之间的一个小数.再通过简单算术运算生成一个符合需求的整数.JS中通用公式通常为parseInt(Ma ...

随机推荐

  1. vim退出后终端保留 退出前的内容

    export TERM=linux 或者 具体选项 可以看看secrecrt的选项

  2. php中替换函数主要用的几个函数strtr(),str_repalce()。

    php中替换函数主要有strtr(),str_repalce()这两个函数,今天介绍下他们的区别和用法, 先来看看这个php字符串替换函数 strtr()的两种用法: strtr(string,fro ...

  3. 转载: scikit-learn学习之K-means聚类算法与 Mini Batch K-Means算法

    版权声明:<—— 本文为作者呕心沥血打造,若要转载,请注明出处@http://blog.csdn.net/gamer_gyt <—— 目录(?)[+] ================== ...

  4. Linux 下安装服务器安全狗

    1.网上下载服务器安全狗的软件包    32位和64位 wget http://www.safedog.cn/server_safedog_linux.html/safedog_linux32.tar ...

  5. mysql日期时间操作

    select curdate(); --获取当前日期 select last_day(curdate()); --获取当月最后一天. select DATE_ADD(curdate(),interva ...

  6. Prototype and Constructor in JavaScript

    The concept of prototype in JavaScript is very confusing, especially to those who come with a C++/JA ...

  7. ngRx 官方示例分析 - 6 - Effect

    @ngrx/effect 前面我们提到,在 Book 的 reducer 中,并没有 Search 这个 Action 的处理,由于它需要发出一个异步的请求,等到请求返回前端,我们需要根据返回的结果来 ...

  8. MyEclipse安装Eclipse Memory Analyzer插件以及使用例子

    一 :安装 1.Memory Analyzer 插件下载地址:http://www.eclipse.org/mat/downloads.php 2.将下载的文件解压到MyEclipse的  dropi ...

  9. SSM-SpringMVC-15:SpringMVC中小论注解式开发之通配符篇

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 此处改了下标题,小论,为什么不说大话了呢?哎,质量不够啊,通配符篇提取不出更多可以讲的滔滔不绝的套路 通配符 ...

  10. QT:QObject 简单介绍

    QObject 是所有Qt对象的基类. QObject 是Qt模块的核心.它的最主要特征是关于对象间无缝通信的机制:信号与槽.使用connect()建立信号到槽的连接,使用disconnect()销毁 ...