1. <tfoot id='sNXH9'></tfoot>

  2. <i id='sNXH9'><tr id='sNXH9'><dt id='sNXH9'><q id='sNXH9'><span id='sNXH9'><b id='sNXH9'><form id='sNXH9'><ins id='sNXH9'></ins><ul id='sNXH9'></ul><sub id='sNXH9'></sub></form><legend id='sNXH9'></legend><bdo id='sNXH9'><pre id='sNXH9'><center id='sNXH9'></center></pre></bdo></b><th id='sNXH9'></th></span></q></dt></tr></i><div id='sNXH9'><tfoot id='sNXH9'></tfoot><dl id='sNXH9'><fieldset id='sNXH9'></fieldset></dl></div>

      <bdo id='sNXH9'></bdo><ul id='sNXH9'></ul>

    <small id='sNXH9'></small><noframes id='sNXH9'>

    1. <legend id='sNXH9'><style id='sNXH9'><dir id='sNXH9'><q id='sNXH9'></q></dir></style></legend>

      Ϊʲô JS ģ̬ÏûÏ¢¿ò»áÔÚ setTimeout() ÉÏÔÝÍ£µ¹¼Æʱ?

      ʱ¼ä£º2023-06-21
    2. <small id='cijMl'></small><noframes id='cijMl'>

      <tfoot id='cijMl'></tfoot>

          <bdo id='cijMl'></bdo><ul id='cijMl'></ul>
              <tbody id='cijMl'></tbody>

              <i id='cijMl'><tr id='cijMl'><dt id='cijMl'><q id='cijMl'><span id='cijMl'><b id='cijMl'><form id='cijMl'><ins id='cijMl'></ins><ul id='cijMl'></ul><sub id='cijMl'></sub></form><legend id='cijMl'></legend><bdo id='cijMl'><pre id='cijMl'><center id='cijMl'></center></pre></bdo></b><th id='cijMl'></th></span></q></dt></tr></i><div id='cijMl'><tfoot id='cijMl'></tfoot><dl id='cijMl'><fieldset id='cijMl'></fieldset></dl></div>
            1. <legend id='cijMl'><style id='cijMl'><dir id='cijMl'><q id='cijMl'></q></dir></style></legend>
                ±¾ÎĽéÉÜÁËΪʲô JS ģ̬ÏûÏ¢¿ò»áÔÚ setTimeout() ÉÏÔÝÍ£µ¹¼Æʱ?µÄ´¦Àí·½·¨£¬¶Ô´ó¼Ò½â¾öÎÊÌâ¾ßÓÐÒ»¶¨µÄ²Î¿¼¼ÛÖµ£¬ÐèÒªµÄÅóÓÑÃÇÏÂÃæËæןú°æÍøµÄС±àÀ´Ò»Æðѧϰ°É£¡

                ÎÊÌâÃèÊö

                µ± alert Ö®ÀàµÄģ̬¶Ô»°¿ò´°¿Ú´ò¿ªÊ±£¬ÎÒÓöµ½ÁË JS setTimeout µÄÒâÍâÐÐΪ£¬ÎÒÏëÖªµÀËü±³ºóµÄÔ­Òò.

                I have encountered an unexpected behavior of JS setTimeout when modal dialog windows like alert are open and I would like to know the reason behind it.

                ÎÒÏ£Íû setTimeout(fn,10000) µÄÒâ˼ÊǶ¨ÆÚ¼ì²éµ±Ç°Ê±¼ä£¬µ±Ëü´óÓÚ Now + 10000ms ʱ´¥·¢½«µ÷Óô«µÝµÄ¡®fn¡¯º¯ÊýµÄʼþ´¦Àí³ÌÐò".Õ⽫ÊǺϺõÂß¼­µÄ£¬¿´¿´ÎÒÃÇÈçºÎ½«³¬Ê±¶ÈÁ¿×÷Ϊ´ÓÏÖÔÚ¿ªÊ¼µÄºÁÃë"´«µÝ.µ«ÊÇ£¬ÏÔÈ»£¬setTimeout Éϵĵ¹¼ÆʱÊÇÎÄ×Öµ¹¼Æʱ£¬²¢ÇÒ»áÔÚģʽ´°¿Ú´ò¿ªÊ±ÔÝÍ£.

                I expected setTimeout(fn,10000) to mean "check current time periodically and when it is greater than Now + 10000ms fire the event handler that will invoke the passed 'fn' function". This would be logical, seeing how we pass the timeout measure as 'ms from now'. But, apparently, the countdown on setTimeout is a literal countdown and will be paused while a modal window is open.

                setTimeout(function(){
                    //alert A
                    alert("10 seconds have passed for the first setTimeout")
                }, 10000);
                setTimeout(function(){
                    //alert B
                    alert("Wait for 15 seconds and press OK");
                },1000);
                

                ÎÒÏ£Íû¾¯±¨ A ÔÚÄú¹Ø±Õ¾¯±¨ B ºóÁ¢¼´ÏÔʾ(¼ÙÉèÄúµÈ´ýÁË 15 Ãë)£¬ÒòΪ¾¯±¨ A ³¬Ê±½ö 10 Ãë²¢ÇÒËüÃÇÒѾ­¹ýÈ¥ÁË.È»¶ø£¬Êµ¼ù±íÃ÷£¬¾¯±¨ A µÄµ¹¼ÆʱֻÊÇÔÚ¾¯±¨ B ´ò¿ªÊ±ÔÝÍ££¬²¢ÇÒ½öÔÚ´óÔ¼ 10 ·ÖÖÓºó²Å»áÏÔʾ.¹Ø±Õ¾¯±¨ B ºóÓÖ¹ýÁË 9 Ã룬ÎÞÂÛ B ´ò¿ªÁ˶೤ʱ¼ä.

                I would expect alert A to display immediately after you close alert B (presuming you waited for 15 sec. to do so), since alert A timeout was just for 10 sec and they have already passed. Practice, however, shows that countdown to alert A is simply paused while alert B is open and it will show only after approx. 9 more seconds have passed after you've closed alert B, no matter how long B was open.

                ÕâËƺõ²»ºÏÂß¼­.

                ¸üÐÂ.ÎÒ¾ø¶Ô²»ÊÇΨһһ¸öÔÚÕâÀï¸Ðµ½À§»óµÄÈË£¬ÒòΪÕâÖÖÔÝÍ£³¬Ê±µÄÐÐΪ·¢ÉúÔÚ Chrome ºÍ Internet Explorer ÖУ¬µ«²»ÊÇ Firefox.Firefox Ö´ÐÐÎÒÔ¤ÆÚµÄÐÐΪ - Èç¹ûÄúÔÚ¾¯±¨ B Éϵȴý 15 Ãë - ¾¯±¨ A »áÔÚÄú¹Ø±ÕËüʱÁ¢¼´µ¯³ö.

                Update. I'm definitely not the only one confused here, because this behavior of pausing the timeout occurs in Chrome and Internet Explorer, but not Firefox. Firefox executes the behavior I expected - if you wait for 15 seconds on alert B - alert A pops out instantly whenever you close it.

                ÍƼö´ð°¸

                ÎÒ»³ÒÉΪʲô IE ºÍ Chrome ¶¼»áÔÝÍ£¹ÒÆðµÄ¼ÆʱÆ÷£¬Ö±µ½ alert ±»½â³ý£¬²¢ÇÒ»ðºüûÓÐ.ÎÒÏàÐÅÕâÖ»ÊÇÒòΪÔÚ½âÊÍ W3C µÄ¹æ·¶·½ÃæÓÐÒ»¶¨µÄ×ÔÓɶȴúÂë>¾¯±¨:

                I doubt there is a definitive answer on why both IE and Chrome put a pause on pending timers until alert has been dismissed, and Firefox doesn't. I believe it's just because there is certain freedom in interpreting the W3C's specs for alert:

                alert(message) ·½·¨ÔÚµ÷ÓÃʱ±ØÐëÖ´ÐÐÒÔϲ½Öè:

                The alert(message) method, when invoked, must run the following steps:

                1. Èç¹ûʼþÑ­»·µÄÖÕֹǶÌ×¼¶±ð²»ÎªÁ㣬Ôò¿ÉÑ¡ÖÐÖ¹ÕâЩ²½Öè.

                1. If the event loop's termination nesting level is non-zero, optionally abort these steps.

                ÊÍ·Å´æ´¢»¥³âËø.

                ÏòÓû§ÏÔʾ¸ø¶¨µÄÏûÏ¢.

                Show the given message to the user.

                (¿ÉÑ¡)ÔڵȴýÓû§È·ÈÏʱÔÝÍ£ÏûÏ¢.

                Optionally, pause while waiting for for the user to acknowledge the message.

                µÚ 4 ²½(ÔÝÍ£)½øÒ»²½½âÊÍ ÕâÀï:

                Step 4 (the pause) is further explained here:

                ±¾¹æ·¶ÖеÄһЩËã·¨£¬ÓÉÓÚÀúÊ·Ô­Òò£¬ÒªÇóÓû§´úÀíÔÚÔËÐÐÈÎÎñʱÔÝÍ££¬Ö±µ½³öÏÖÌõ¼þÄ¿±ê´ï³É.ÕâÒâζ×ÅÔËÐÐÒÔϲ½Öè:

                Some of the algorithms in this specification, for historical reasons, require the user agent to pause while running a task until a condition goal is met. This means running the following steps:

                1. Èç¹ûÈκÎÒì²½ÔËÐеÄËã·¨ÕýÔڵȴýÎȶ¨×´Ì¬£¬È»ºóÔËÐÐËûÃǵÄͬ²½²¿·Ö£¬È»ºó¼ÌÐøÔËÐÐËûÃǵÄÒì²½Ëã·¨.(²Î¼û ʼþÑ­»· ´¦ÀíÄ£ÐÍÏêÇé¼ûÉÏÎĶ¨Òå.)

                1. If any asynchronously-running algorithms are awaiting a stable state, then run their synchronous section and then resume running their asynchronous algorithm. (See the event loop processing model definition above for details.)

                ÈçÓбØÒª£¬¸üÐÂÈκÎÎĵµµÄ³ÊÏÖ»òÓû§½çÃæ»òä¯ÀÀÉÏÏÂÎÄÒÔ·´Ó³µ±Ç°×´Ì¬.

                If necessary, update the rendering or user interface of any Document or browsing context to reflect the current state.

                µÈµ½Âú×ãÌõ¼þÄ¿±ê.µ±Óû§´úÀíÔÝͣʱÈÎÎñ£¬ÏàÓ¦µÄʼþÑ­»·²»µÃÔËÐнøÒ»²½µÄÈÎÎñ£¬ÒÔ¼°Èκε±Ç°ÔËÐÐÈÎÎñÖеĽű¾±ØÐë×èÈû.Óû§´úÀíÓ¦¸ÃÈ»¶ø£¬ÔÚÔÝͣʱÈÔÈ»ÏìÓ¦Óû§ÊäÈ룬¾¡¹ÜÔÚÈÝÁ¿¼õÉÙ£¬ÒòΪʼþÑ­»·²»»á×öÈκÎÊÂÇé.

                Wait until the condition goal is met. While a user agent has a paused task, the corresponding event loop must not run further tasks, and any script in the currently running task must block. User agents should remain responsive to user input while paused, however, albeit in a reduced capacity since the event loop will not be doing anything.

                Òò´Ë£¬ÎÞÂÛÈçºÎ£¬Ê¼þÑ­»·¶¼»áÔÝÍ£.µ±¾¯±¨ÈÔÈ»¿É¼ûºÍģ̬ʱ£¬²»»áµ÷Óýϳ¤³¬Ê±µÄ»Øµ÷.Èç¹û²»ÊÇÕâÑù£¬¿ÉÄÜ»á³öÏÖ¸÷ÖÖÁîÈËÌÖÑáµÄÊÂÇ飬±ÈÈç¶à¸ö¾¯±¨µþ¼ÓÔÚÒ»Æð.

                So, the event loop gets suspended in any case. The callback of the longer timeout doesn't get invoked while the alert is still visible and modal. Had it not been this way, all kinds of nasties might be made possible, like multiple alerts on top of each other.

                ÏÖÔÚ£¬ÄúÄÜ·ñ´ÓÉÏÊö¹æ·¶ÖÐÅжϼÆʱÆ÷µ¹¼ÆʱÊÇ·ñÓ¦¸ÃÔÚ¾¯±¨µÄÉúÃüÖÜÆÚÄÚÔÝÍ££¬»¹ÊÇÓ¦¸ÃÔÚ¾¯±¨ÏûʧºóÁ¢¼´´¥·¢?ÎÒ²»ÄÜ£¬ÎÒʲÖÁ²»È·¶¨ÄÄÖÖÐÐΪ¸üºÏºõÂß¼­.

                Now, can you tell from the above specs if the timer countdown should be suspended for the lifetime of the alert, or should rather be fired as soon as the alert has gone? I can't, and I'm not even sure which behavior would be more logical.

                ÎÒ¿ÉÒԿ϶¨µÄÊÇ£¬³ýÁ˵÷ÊÔÄ¿µÄÖ®Í⣬Äú²»Ó¦¸Ã½« JavaScript ¾¯±¨ÓÃÓÚÆäËûÈκÎÄ¿µÄ.¾¯±¨È·ÊµÔÊÐíÔÝÍ£½Å±¾Ö´ÐÐ(¶ø XHR µÈһЩÒì²½²Ù×÷ÕýÔÚºǫ́½øÐÐ)£¬µ«ËüÃǶÔÓû§·Ç³£²»ÓѺÃ.ÕýÈ·µÄ·½·¨ÊDzÉÓÃÒì²½´úÂ룬ʹÓà Promise£¬¿ÉÄÜ»¹ÓÐ ES6 generators/yeild(Èç¹ûÄãϲ»¶ÏßÐÔ´úÂë·ç¸ñ).

                What I'm sure about is that you shouldn't be using JavaScript alerts for anything else but debugging purposes. Alerts do allow to suspend script execution (while some asynchronous operation like XHR is taking place in the background), but they're quite unfriendly to the user. The right approach would be embrace asynchronous code, using promises and, possibly, ES6 generators/yeild (if you're after the linear code style).

                ÒÔÏÂÎÊÌâ¸ß¶ÈÏà¹Ø£¬´Ë´¦ÌÖÂÛÁË alert µÄһЩÌæ´ú·½°¸:

                The following question is highly related and some alternatives to alert are discussed there:

                • ·ÀÖ¹ js alert() ÔÝÍ£¼ÆʱÆ÷

                Õâƪ¹ØÓÚΪʲô JS ģ̬ÏûÏ¢¿ò»áÔÚ setTimeout() ÉÏÔÝÍ£µ¹¼Æʱ?µÄÎÄÕ¾ͽéÉܵ½ÕâÁË£¬Ï£ÍûÎÒÃÇÍƼöµÄ´ð°¸¶Ô´ó¼ÒÓÐËù°ïÖú£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö¸ú°æÍø£¡

                ÉÏһƪ£ºBootstrap 3/ÏÔʾģʽ²»ÊÊÓÃÓÚjavascript·½Ê½ ÏÂһƪ£ºÈçºÎÇå³ý»­²¼ÒÔ½øÐÐÖØ»æ

                Ïà¹ØÎÄÕÂ

                • <bdo id='knsWw'></bdo><ul id='knsWw'></ul>
                <tfoot id='knsWw'></tfoot>

                <legend id='knsWw'><style id='knsWw'><dir id='knsWw'><q id='knsWw'></q></dir></style></legend>

                1. <i id='knsWw'><tr id='knsWw'><dt id='knsWw'><q id='knsWw'><span id='knsWw'><b id='knsWw'><form id='knsWw'><ins id='knsWw'></ins><ul id='knsWw'></ul><sub id='knsWw'></sub></form><legend id='knsWw'></legend><bdo id='knsWw'><pre id='knsWw'><center id='knsWw'></center></pre></bdo></b><th id='knsWw'></th></span></q></dt></tr></i><div id='knsWw'><tfoot id='knsWw'></tfoot><dl id='knsWw'><fieldset id='knsWw'></fieldset></dl></div>

                  <small id='knsWw'></small><noframes id='knsWw'>