欢迎光临散文网 会员登陆 & 注册

碎片时间学编程「273]:延迟函数调用

2023-02-23 12:19 作者:路条编程  | 我要投稿


延迟函数调用


推迟调用函数,直到当前调用堆栈已清除。

使用 setTimeout() 方法设置  1 毫秒  超时将新事件添加到事件队列并允许渲染引擎完成其工作。

使用扩展 ( ...) 运算符为函数提供任意数量的参数。


JavaScript

const defer = (fn, ...args) => setTimeout(fn, 1, ...args);


示例:

// 示例 A:

defer(console.log, 'a'), console.log('b'); // logs 'b' then 'a'


// 示例 B:

document.querySelector('#someElement').innerHTML = 'Hello';

longRunningFunction();

// 在完成之前浏览器不会更新 HTML

defer(longRunningFunction);

// 浏览器将更新 HTML 然后运行函数

更多内容请访问我的网站:https://www.icoderoad.com


碎片时间学编程「273]:延迟函数调用的评论 (共 条)

分享到微博请遵守国家法律