小程序开放平台

文档中心
基础
路由
跳转
转发
界面
网络
支付
数据缓存
媒体
位置
开放接口
设备
XHSML
性能
getPerformance
getEntries
getEntriesByName
getEntriesByType
getCurrentPageEntries
getEntriesByPage
mark
clearMarks
PerformanceEntry
createObserver
PerformanceObserver
第三方平台
文件
数据分析
画布

xhs.createObserver

开发
>
JS API
>
性能
>
createObserver
>
更新时间:2024-11-13 19:05:31

创建全局性能事件监听器

参数

function callback

回调函数

返回值

PerformanceObserver

// page/index/index
Page({
    onLoad() {
        // 假如业务方想统计fp、fcp、fmp
        const performance = xhs.getPerformance()
        this.observer = performance.createObserver((entryList) => {
                const entries = entryList.getEntries()
                this.report(entries)
          })
        this.observer.observe({ entryTypes: ['paint'] })
        // or this.observer.observe({ type: 'paint' })
    },
 
 
    report(entries) {
        const fp = entries.filter(item => item.name === 'firstPaint')[0]
        if (fp) {
            const { 
                startTime, // fp的时间戳
                duration,  // 无
                value,  // performance.timing.navigationStart - startTime的这段时间
             } = fp
        }
        // 业务方do something
     },
 
    onUnload() {
        this.observer.disconnect()
    }
})
该文档是否对您有帮助?