[翻译]High Performance JavaScript(020)
Yielding with Timers 用定时器让出时间片
Despite your best efforts, there will be times when a JavaScript task cannot be completed in 100 milliseconds or less because of its complexity. In these cases, it's ideal to yield control of the UI thread so that UI updates may occur. Yielding control means stopping JavaScript execution and giving the UI a chance to update itself before continuing to execute the JavaScript. This is where JavaScript timers come into the picture.
尽管你尽了最大努力,还是有一些JavaScript任务因为复杂性原因不能在100毫秒或更少时间内完成。这种情况下,理想方法是让出对UI线程的控制,使UI更新可以进行。让出控制意味着停止JavaScript运行,给UI线程机会进行更新,然后再继续运行JavaScript。于是JavaScript定时器进入了我们的视野。
Timer Basics 定时器基础
Timers are created in JavaScript using either setTimeout() or setInterval(), and both accept the same arguments: a function to execute and the amount of time to wait (in milliseconds) before executing it. The setTimeout() function creates a timer that executes just once, whereas the setInterval() function creates a timer that repeats periodically.
在JavaScript中使用setTimeout()或setInterval()创建定时器,两个函数都接收一样的参数:一个要执行的函数,和一个运行它之前的等待时间(单位毫秒)。setTimeout()函数创建一个定时器只运行一次,而setInterval()函数创建一个周期性重复运行的定时器。
The way that timers interact with the UI thread is helpful for breaking up long-running scripts into shorter segments. Calling setTimeout() or setInterval() tells the JavaScript engine to wait a certain amount of time and then add a JavaScript task to the UI queue. For example:
定时器与UI线程交互的方式有助于分解长运行脚本成为较短的片断。调用setTimeout()或setInterval()告诉JavaScript引擎等待一定时间然后将JavaScript任务添加到UI队列中。例如:
function greeting(){
alert("Hello world!");
}
setTimeout(gree
相关文档:
Closure中文翻译为闭包.字面上来理解就是"封闭的包".(这是一句废话)
闭包是什么?
书面解释为:
所谓“闭包”,指的是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分。
我认为闭包就是能够读/写函数内部的某些变量的子函数,并将这些变量保存 ......
常规的方法是将年月日取出,然后分别判断范围,然后就判断闰年2月的天数
可以通过new Date(string)的构造,比较年月日字符是否发生变化判断。
function CheckDate(text) {
if (!text) return false;
text = text.replace(/[\/-]0?/g, "/");
if (!text.match(/^\d{4}\/\d{1,2}\/\d{1,2}$/)) return true;
......
AJAX (异步 JavaScript 和 XML) 是个新产生的术语,专为描述JavaScript的两项强大性能.这两项性
能在多年来一直被网络开发者所忽略,直到最近Gmail, Google suggest和google Maps的横空出世才使人
们开始意识到其重要性.
这两项被忽视的性能是:
* 无需重新装载整个页面便能向服务器发送请求.
* 对XML文档的解析和处理.
......
Conditionals 条件表达式
Similar in nature to loops, conditionals determine how execution flows through JavaScript. The traditional argument of whether to use if-else statements or a switch statement applies to JavaScript just as it does to other languages. Since different b ......
第五章 Strings and Regular Expressions 字符串和正则表达式
Practically all JavaScript programs are intimately tied to strings. For example, many applications use Ajax to fetch strings from a server, convert those strings into more easily usable JavaScript objects, and ......