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


Lodash _.after()用法及代碼示例

Lodash _.after()方法與Lodash _.before()方法相反。此方法用於創建一個函數,該函數在被調用n次或多次後將調用func。

用法:

_.after(n, func)

參數:該方法接受上述和以下所述的兩個參數:

  • n:此參數保留數字n,該數字定義在調用func之前的調用次數。
  • func:此參數保存將要調用的函數。

返回值:此方法返回新的受限函數。

下麵的示例說明JavaScript中的Lodash _.after()方法。



範例1:在此示例中,我們嘗試調用該函數3次,但僅將其調用3次。

// Requiring the lodash library   
const _ = require("lodash");  
  
// Applying _.after() method 
var gfg = _.after(3, function () { 
  console.log('Saved'); 
}); 
  
gfg(); // Print nothing 
gfg(); // Print nothing 
gfg(); // Print Saved

輸出:

Saved

範例2:在此示例中,我們嘗試兩次調用該函數,但僅第二次調用。

// Requiring the lodash library   
const _ = require("lodash");  
  
// Applying _.after() method 
var gfg = _.after(2, function () { 
  console.log('Successful'); 
}); 
  
gfg(); // Print nothing 
gfg(); // Print Successful

輸出:

Successful

參考:https://docs-lodash.com/v4/after/

相關用法


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