當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


underscore.js _.delay()用法及代碼示例

_.delay() 函數在等待指定的毫秒後執行其參數中提到的函數。它主要用於我們想要執行某些任務但經過一定時間之後。在這種情況下,我們可以定義這個函數,然後等待毫秒後執行。如果我們也將參數傳遞給這個函數(這是可選的傳遞),那麽這些參數將作為傳遞給 _.delay() 函數的函數的參數。

用法:

_.delay(function, wait, *arguments)

參數:它需要三個參數:

  • function:要執行的函數。
  • wait:函數需要執行的時間(以毫秒為單位)
  • *arguments:傳遞給 _.delay() 函數的函數參數(可選)

返回值:它返回傳遞的函數的值,等待毫秒後執行。

例子:

  1. 將函數直接傳遞給 _.delay() 函數:_.delay() 函數接受等待參數,這裏是 1000ms,然後等待 1000ms,然後執行傳遞的函數,這裏是 console.log() 並打印傳遞給它的字符串,即編碼很有趣。因此,在 1000 毫秒後,將顯示字符串 “coding is fun”。
    
    <html>
       
    <head>
        <!-- These lines are for Mozilla Firefox 
              developer edition to stop the web packs-->
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <!-- You may ignore these when using in another browser -->
        <script src = 
    "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            _.delay(console.log, 1000, 'coding is fun!');
        </script>
    </body>
       
    </html>

    輸出:

  2. 將 _.bind() 函數與 _.delay() 函數一起使用:
    _.bind() 函數用於將對象傳遞給函數。就像這裏的 console.log() 函數有一個對象控製台。這個“func()”意味著將傳遞給該函數的任何內容都將顯示在控製台上。 _.bind() 函數中沒有提到等待時間。然後在 _.delay() 函數中,我們需要等待 2000 毫秒,然後字符串 “hello” 將顯示在控製台上。
    
    <html>
       
    <head>
        <!-- These lines are for Mozilla Firefox 
           developer edition to stop the web packs-->
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <!-- You may ignore these when using in another browser -->
        <script src = 
    "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
        </script>
        <script src=
        "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            var func = _.bind(console.log, console);
            _.delay(func, 2000, 'hello');
        </script>
    </body>
       
    </html>

    輸出:

  3. 將多個參數傳遞給傳遞給 _.delay() 函數的函數:
    _.delay() 函數有一個“func()”傳遞給它,其中包含與前麵示例中相同的 _.bind() 函數。然後經過 3000 毫秒的等待時間,這意味著輸出將在 3000 毫秒後顯示。還有 3 個參數被傳遞,它們將被視為傳遞的“func()”函數的參數。因此,最終輸出將在 3000 毫秒後顯示,並且將是所有 3 個字符串的組合,即“你好!你好嗎?”。
    
    <html>
       
    <head>
        <!-- These lines are for Mozilla Firefox 
           developer edition to stop the web packs-->
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
      
        <!-- You may ignore these when using in another browser -->
        <script src = 
    "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
        </script>
        <script src=
        "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            var func = _.bind(console.log, console);
            _.delay(func, 3000, 'hello!', 'how are', 'you?');
        </script>
    </body>
       
    </html>

    輸出:

  4. 將數字作為參數傳遞給傳遞函數到_.delay()函數:
    我們甚至可以將數字作為參數傳遞給他傳遞的函數。在這裏,我們將 ‘12345’ 作為參數傳遞給“func()”函數。 “func()”函數的聲明方式與前麵的示例一樣。此函數的輸出將是 “12345”,它將在 5000 毫秒後顯示。
    
    <html>
       
    <head>
        <!-- These lines are for Mozilla Firefox 
             developer edition to stop the web packs-->
        <meta content="text/html;charset=utf-8" 
         http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
      
        <!-- You may ignore these when using in another browser -->
        <script src = 
    "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
        </script>
        <script src=
        "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            var func = _.bind(console.log, console);
            _.delay(func, 5000, '12345');
        </script>
    </body>
       
    </html>

    輸出:

注意這些命令在 Google 控製台或 Firefox 中不起作用,因為需要添加他們沒有添加的這些附加文件。
因此,將給定的鏈接添加到您的 HTML 文件中,然後運行它們。

鏈接如下:


<!-- Write HTML code here -->
<script type="text/javascript" src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>



相關用法


注:本文由純淨天空篩選整理自Sakshi98大神的英文原創作品 Underscore.js _.delay() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。