×ðÁú¿­Ê±¹ÙÍøµÇ¼

jsÖÐÔõÑùsleepÒ»Ãë

javascript ÖÐµÄ sleep ÒªÁ죺1. settimeout()£ºÑÓ³ÙÖ´Ðк¯Êý£¬ÈõµãÊÇÑÓ³Ùʱ¼ä½û¾øÈ·£»2. promise£º½¨ÉèÒì²½Íê³ÉµÄ promise£¬ÓŵãÊÇÑÓ³Ùʱ¼ä׼ȷ£¬¿É×éºÏÆäËûÒì²½²Ù×÷£»3. async/await£º±àд¿´ÆðÀ´Í¬²½µÄÒì²½´úÂ룬ÓŵãÊǿɶÁÐԸߣ¬ÈõµãÊÇÖ»ÄÜÔÚ´øÓÐ async Òªº¦×ֵĺ¯ÊýÖÐʹÓá£

JavaScript ÖÐÔõÑù Sleep Ò»Ãë

ÔÚ JavaScript ÖУ¬Ã»ÓÐÄÚÖÃµÄ sleep º¯Êý¡£È»¶ø£¬Óм¸ÖÖÒªÁì¿ÉÒÔʵÏÖÀàËƵÄÐÐΪ£º

setTimeout()

setTimeout() º¯ÊýÔÊÐíÄúÍýÏëÔÚ¸ø¶¨µÄÑÓ³ÙºóÖ´Ðк¯Êý¡£Òª Sleep Ò»Ã룬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺

setTimeout(() => {
  // ÔÚÕâÀïÖ´ÐÐ Sleep ºóµÄ´úÂë
}, 1000);

µÇ¼ºó¸´ÖÆ

Óŵ㣺

¼òÆÓÒ×Óá£

Èõµã£º

ÑÓ³Ùʱ¼ä½û¾øÈ·£¬¿ÉÄܱ£´æÎó²î¡£

Promise

Promise ¿ÉÒÔÓÃÀ´ÌåÏÖÒì²½²Ù×÷µÄÍê³É¡£Òª Sleep Ò»Ã룬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺

const sleepPromise = new Promise((resolve) => {
  setTimeout(() => {
    resolve();
  }, 1000);
});

sleepPromise.then(() => {
  // ÔÚÕâÀïÖ´ÐÐ Sleep ºóµÄ´úÂë
});

µÇ¼ºó¸´ÖÆ

Óŵ㣺

ÑÓ³Ùʱ¼ä¸ü׼ȷ¡£

¿ÉÒÔÇáËÉ×éºÏÆäËûÒì²½²Ù×÷¡£

Èõµã£º

Óï·¨ÉÔÏÔÖØ´ó¡£

async/await

async/await Óï·¨ÔÊÐíÄú±àд¿´ÆðÀ´Í¬²½µÄÒì²½´úÂë¡£Òª Sleep Ò»Ã룬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺

async function sleep() {
  await new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, 1000);
  });
}

sleep().then(() => {
  // ÔÚÕâÀïÖ´ÐÐ Sleep ºóµÄ´úÂë
});

µÇ¼ºó¸´ÖÆ

Óŵ㣺

´úÂë¿É¶ÁÐԸߣ¬¿´ÆðÀ´Ïñͬ²½´úÂë¡£

Èõµã£º

Ö»ÄÜÔÚ´øÓÐ async Òªº¦×ֵĺ¯ÊýÖÐʹÓá£

ÒÔÉϾÍÊÇjsÖÐÔõÑùsleepÒ»ÃëµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ