📜  Lodash _.delay() 方法

📅  最后修改于: 2022-05-13 01:56:49.438000             🧑  作者: Mango

Lodash _.delay() 方法

Lodash是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。

_.delay() 方法用于在规定的等待时间结束后调用给定函数作为参数,以毫秒为单位。调用该函数时,会向该函数提供任何进一步的参数。

语法

_.delay( func, wait, args )

参数:此方法接受三个参数,如上所述,如下所述:

  • func:必须延迟的函数。
  • wait:函数调用延迟的毫秒数。
  • args:它是调用给定函数的参数。它是一个可选参数。

返回值:此方法返回定时器 id。

示例 1:在本示例中,由于等待时间为 3 秒,因此在延迟 3 秒后打印内容。

Javascript
// Requiring lodash library
const _ = require('lodash');
  
// Using the _.delay() method
// with its parameter
_.delay(function(content) {
    console.log(content);
  }, 3000, 'GeeksforGeeks!');
  
// Print the content after this line
console.log('Content:');


Javascript
// Requiring lodash library
const _ = require('lodash');
  
// Defining func parameter
let func = number => {
  console.log(number);
};
  
// Defining for loop
for(let i = 1; i <= 5; i++) {
      
    // Using the _.delay() method
    // with its parameter
    _.delay(func, 2000 * (i + 1), i);
}
  
// Prints the integer after this line
console.log('Integers are as follows:');


输出:

Content:
GeeksforGeeks!

示例 2:在此示例中,每个整数在 2 秒延迟后打印。

Javascript

// Requiring lodash library
const _ = require('lodash');
  
// Defining func parameter
let func = number => {
  console.log(number);
};
  
// Defining for loop
for(let i = 1; i <= 5; i++) {
      
    // Using the _.delay() method
    // with its parameter
    _.delay(func, 2000 * (i + 1), i);
}
  
// Prints the integer after this line
console.log('Integers are as follows:');

输出:

Integers are as follows:
1
2
3
4
5