javascript 推迟JS函数调用直到浏览器有机会呼吸(https://github.com/TehShrike/idle-until-urgent)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 推迟JS函数调用直到浏览器有机会呼吸(https://github.com/TehShrike/idle-until-urgent)相关的知识,希望对你有一定的参考价值。
function idleExec(fn, affectsDOM, timeoutFallbackMs = 500) {
const root = (typeof window != 'undefined' ? window : global)
let cancel
let result
const thunk = () => { cancel = null; result = fn(); }
if (affectsDOM && ('requestAnimationFrame' in root)) {
const handleId = requestAnimationFrame(() => requestAnimationFrame(thunk))
cancel = () => cancelAnimationFrame(handleId)
} else if ('requestIdleCallback' in root) {
const handleId = requestIdleCallback(thunk)
cancel = () => cancelIdleCallback(handleId)
} else {
const handleId = setTimeout(thunk, timeoutFallbackMs)
cancel = () => clearTimeout(handleId)
}
return () => {
if (cancel !== null) {
cancel()
result = fn()
}
return result
}
}
/*
var getFormatter = idleExec(() =>
new Intl.DateTimeFormat('en-US', {
timeZone: 'America/Los_Angeles',
}))
// later in your code, presumably not during the first tick...
var delay = ms => new Promise(res => setTimeout(res, ms))
await delay(1000)
getFormatter().format(new Date(1537452915210)) // => '9/20/2018'
*/
以上是关于javascript 推迟JS函数调用直到浏览器有机会呼吸(https://github.com/TehShrike/idle-until-urgent)的主要内容,如果未能解决你的问题,请参考以下文章
浏览器中的JavaScript事件循环机制
如何推迟调用@PostConstruct,直到jUnit设置测试上下文
浏览器解析js的顺序
推迟执行连接到控制事件的 Javascript 以支持外部 jQuery 函数
javascript怎样调用php函数?
javascript函数怎么自己调用自己啊?