import { _decorator, Component, Node } from 'cc'; import { Singleton } from './Singleton'; const { ccclass, property } = _decorator; @ccclass('FrameExecute') export class FrameExecute extends Component { //单例 private static _instance: FrameExecute; public static getInstance(): FrameExecute { return Singleton.getInstance(FrameExecute); } /** * 分帧执行 * @param fun 执行函数 * @param target 执行函数对象 * @param start 起始次数 (从0开始,0表示执行第1次) * @param max 最大执行次数 (例如5则表示函数总共执行5次) * @param executeTime 分配的执行时间 * @param data 传递数据 * @returns */ public execute(fun: Function, target: any, start: number, max: number, executeTime: number, data: any = null) { //获取开始时间 let startTime = new Date().getTime(); //执行计数 let count = start; //开始执行函数,如果超过分配的执行时间,则延迟到下一帧执行 while (count < max) { //执行函数 fun.call(target, count, data); //获取消耗时间 var costTime = new Date().getTime() - startTime; console.log("执行耗时:", costTime); //消耗时间 > 分配的时间,则延迟到下一帧执行 if (costTime > executeTime) { console.log("超时,进入下一轮加载"); this.scheduleOnce(() => { this.execute(fun, target, count + 1, max, executeTime, data) }); return; } count++; } } }