Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

[·­Òë]High Performance JavaScript(021)

Splitting Up Tasks  ·Ö½âÈÎÎñ
    What we typically think of as one task can often be broken down into a series of subtasks. If a single function is taking too long to execute, check to see whether it can be broken down into a series of smaller functions that complete in smaller amounts of time. This is often as simple as considering a single line of code as an atomic task, even though multiple lines of code typically can be grouped together into a single task. Some functions are already easily broken down based on the other functions they call. For example:
    ÎÒÃÇͨ³£½«Ò»¸öÈÎÎñ·Ö½â³ÉһϵÁÐ×ÓÈÎÎñ¡£Èç¹ûÒ»¸öº¯ÊýÔËÐÐʱ¼äÌ«³¤£¬ÄÇô²é¿´ËüÊÇ·ñ¿ÉÒÔ·Ö½â³ÉһϵÁÐÄܹ»¶Ìʱ¼äÍê³ÉµÄ½ÏСµÄº¯Êý¡£¿É½«Ò»ÐдúÂë¼òµ¥µØ¿´×÷Ò»¸öÔ­×ÓÈÎÎñ£¬¶àÐдúÂë×éºÏÔÚÒ»Æð¹¹³ÉÒ»¸ö¶ÀÁ¢ÈÎÎñ¡£Ä³Ð©º¯Êý¿É»ùÓÚº¯Êýµ÷ÓýøÐвð·Ö¡£ÀýÈ磺
function saveDocument(id){
  //save the document
  openDocument(id)
  writeText(id);
  closeDocument(id);
  //update the UI to indicate success
  updateUI(id);
}
    If this function is taking too long, it can easily be split up into a series of smaller steps by breaking out the individual methods into separate timers. You can accomplish this by adding each function into an array and then using a pattern similar to the array-processing pattern from the previous section:
    Èç¹ûº¯ÊýÔËÐÐʱ¼äÌ«³¤£¬Ëü¿ÉÒÔ²ð·Ö³ÉһϵÁиüСµÄ²½Ö裬°Ñ¶ÀÁ¢·½·¨·ÅÔÚ¶¨Ê±Æ÷Öе÷Óá£Äã¿ÉÒÔ½«Ã¿¸öº¯Êý¶¼·ÅÈëÒ»¸öÊý×飬ȻºóʹÓÃÇ°Ò»½ÚÖÐÌáµ½µÄÊý×é´¦Àíģʽ£º
function saveDocument(id){
  var tasks = [openDocument, writeText, closeDocument, updateUI];
  setTimeout(function(){
    //execute the next task
    var task = tasks.shift();
    task(id);
    //determine if there's more
    if (tasks.length > 0){
      setTimeout(arguments.callee, 25);
    }
  }, 25);
}
  


Ïà¹ØÎĵµ£º

javascript¼Ì³Ð·½Ê½Ö®Èý

3¡¢×éºÏ¹¹Ô캯Êý/Ô­ÐÍ·½Ê½Ð´À࣬²ÉÓÃÇ°ÃæÖÖ·½Ê½¼Ì³Ð
ÕâÖÖ·½Ê½¸¸À࣬×ÓÀàµÄÊôÐÔ¶¼¹ÒÔÚ¹¹Ô캯ÊýÀ·½·¨¶¼¹ÒÔÚÔ­ÐÍÉÏ¡£
/**
* ¸¸ÀàPolygon:¶à±ßÐÎ
*/
function Polygon(sides) {
this.sides = sides;
}
Polygon.prototype.setSides = function(s) {this.sides=s;}
/**
* Triangle Èý½ÇÐÎ
* @param {Object} b ......

javaScript¹Ø±Õä¯ÀÀÆ÷ (²»µ¯³öÌáʾ¿ò)

£¼script language="javaScript"£¾
function closeWindow()
{
¡¡window.opener = null;
¡¡window.open(' ', '_self', ' ');
¡¡window.close();
}
£¼/script£¾
£¼input type='button' value='¹Ø±Õ´°¿Ú' onClick="closeWindow()"£¾
»ò
£¼input type="button" value="¹ ......

AJAX (Òì²½ JavaScript ºÍ XML)

AJAX (Òì²½ JavaScript ºÍ XML) ÊǸöвúÉúµÄÊõÓï,רΪÃèÊöJavaScriptµÄÁ½ÏîÇ¿´óÐÔÄÜ.ÕâÁ½ÏîÐÔ
ÄÜÔÚ¶àÄêÀ´Ò»Ö±±»ÍøÂ翪·¢ÕßËùºöÂÔ,Ö±µ½×î½üGmail, Google suggestºÍgoogle MapsµÄºá¿Õ³öÊÀ²ÅʹÈË
ÃÇ¿ªÊ¼Òâʶµ½ÆäÖØÒªÐÔ.
ÕâÁ½Ïî±»ºöÊÓµÄÐÔÄÜÊÇ:
* ÎÞÐèÖØÐÂ×°ÔØÕû¸öÒ³Ãæ±ãÄÜÏò·þÎñÆ÷·¢ËÍÇëÇó.
* ¶ÔXMLÎĵµµÄ½âÎöºÍ´¦Àí£® ......

javascriptʵÏÖ×Ô¶¯ÇóºÍ

function total(){
var i=0;
for(j=1;j<=20;j++)
{
var step="step"+j;
if(document.getElementById(step)){
if(document.getElementById(step).checked==true)
{
i=i+parseInt(document.getElementById(step).value);
}
}
}
document.getElementById("total").innerHTML = i;
}
function Resetvalue(){
......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ