[·Òë]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
Ïà¹ØÎĵµ£º
ÍæPHP¡¢Delphi¡¢Java»ù±¾É϶¼ÓжÔÏó£¬Ï°¹ßÕâÖÖ˼·ºóÉÏÊÖÈκÎÓïÑÔ¶¼Ïë¿¿OO˼·£¬Õâ¾ø²»ÊÇÔÚ¸Ïʱ÷Ö£¬¶øÊÇ°ÑÏà¹Ø´úÂë½øÐÐÄÚ¾ÛµÄÈ·¿ÉÒÔÌå»áµ½Î¬»¤µÄ·½±ã£¡
ÔÚJavaScriptÖÐÈçºÎ´´½¨¶ÔÏó£¿
JavaScriptÊÇ»ùÓÚ¶ÔÏóµÄ£¡ËüÒ²ÊÇÒÔObjectΪ¸ùÀ࣬ÆäËüÀà¼Ì³ÐÖ®¡£ÔÚ¸ùÀàÌṩÁ˼¸¸ö·½·¨¡£¹©¼Ì³ÐÀàʹÓã¡
ÒÔÏÂÊÇ´´½¨¶ÔÏóµÄÀý×Ó£º
funct ......
ClosureÖÐÎÄ·ÒëΪ±Õ°ü.×ÖÃæÉÏÀ´Àí½â¾ÍÊÇ"·â±ÕµÄ°ü".(ÕâÊÇÒ»¾ä·Ï»°)
±Õ°üÊÇʲô?
ÊéÃæ½âÊÍΪ:
Ëùν“±Õ°ü”£¬Ö¸µÄÊÇÒ»¸öÓµÓÐÐí¶à±äÁ¿ºÍ°ó¶¨ÁËÕâЩ±äÁ¿µÄ»·¾³µÄ±í´ïʽ£¨Í¨³£ÊÇÒ»¸öº¯Êý£©£¬Òò¶øÕâЩ±äÁ¿Ò²ÊǸñí´ïʽµÄÒ»²¿·Ö¡£
ÎÒÈÏΪ±Õ°ü¾ÍÊÇÄܹ»¶Á/дº¯ÊýÄÚ²¿µÄijЩ±äÁ¿µÄ×Óº¯Êý,²¢½«ÕâЩ±äÁ¿±£´æ ......
typeof ÊÇJavaScriptµÄÔËËã·û
——·µ»ØÒ»¸öÓÃÀ´±íʾ±í´ïʽµÄÊý¾ÝÀàÐ͵Ä×Ö·û´®
typeof[(] expression [)] //typeofÓï·¨ÖеÄÔ²À¨ºÅÊÇ¿ÉÑ¡Ïî
typeof ÔËËã·û@import url(../html-vss/msdnie4a.css);
typeof
ÔËËã·û°ÑÀàÐÍÐÅÏ¢µ±×÷×Ö·û´®·µ»Ø¡£typeof
·µ»ØÖµÓÐÁ ......
µÚÁùÕ Responsive Interfaces ÏìÓ¦½Ó¿Ú
There's nothing more frustrating than clicking something on a web page and having nothing happen. This problem goes back to the origin of transactional web applications and resulted in the now-ubiquitous "please click only once" m ......