一 : 前景
有很多前端可能覺得setTimeOut不值得一講,But,在不同的平臺setTimeOut的執行是有一定的規則需要遵守,有時也叫迫於無奈.比如,前幾日的開發中發現一個非常奇怪的現象 , 就是setOutTime在Web / 微信IDE端都是可以起作用的 , 但是一旦到了手機微信中運行,好似就不那麼靈光了.今日抽了點時間好好測試了一番 , 內容如下:
二 : 測試
Ⅰ,測試環境如下(使用TS代碼)
Ⅱ,代碼
1,測試思想
分別給出有參數(setTimeOut自帶傳參和setTimeOut不自帶) , 無參數在Web( edge ) , 微信IDE , 手機微信中查看各自的結果(打印數據)
2,代碼
①,三種情況的枚舉代碼
export enum TY_SETTIMEOUT {
/**setTimeOut 不自帶參數*/
___HAS_PARAMS_UNBRING4ALONG___ = 1,
/**不用傳參*/
___NO_PARAMS___ = 2,
/**setTimeOut 自帶參數*/
___HAS_PARAMS_BRING4ALONG___ = 3
}
②,核心測試代碼
private _myName : string = null;
private _myAge : number = null;
/**
* 在微信端測試
*/
private testSetTimeOutInWeChat( $ty : TY_SETTIMEOUT ) : void{
switch ($ty) {
case TY_SETTIMEOUT.___NO_PARAMS___:
this._timeout_id = setTimeout(
this.onTimeOutNoParams.bind(this),
2000
);
break;
case TY_SETTIMEOUT.___HAS_PARAMS_UNBRING4ALONG___:
this._myName = "Aonaufly";
this._myAge = 18;
this._timeout_id = setTimeout(
this.onTimeOutHasParams_2_unbring.bind(this),
2000
);
break;
case TY_SETTIMEOUT.___HAS_PARAMS_BRING4ALONG___:
this._timeout_id = setTimeout(
this.onTimeOutHasParams_2_bring.bind(this),
2000,
"Aonaufly",
18
);
break;
}
}
private onTimeOutNoParams() : void {
console.log( ` no params timeout 結束 ------` );
}
private onTimeOutHasParams_2_bring( $name : string , $age : number ) : void{
console.log(`has params timeout 自帶參數 結束 ------ , name : ${$name} , age : ${$age}`);
}
private onTimeOutHasParams_2_unbring() : void{
console.log(`has params timeout 不自帶參數 結束 ------ , name : ${this._myName} , age : ${this._myAge}`);
}
Ⅲ,測試
1,不帶參數.
①WEB端
②微信IDE
③手機微信
2,setTimeOut自帶參數
①WEB
②微信IDE
③手機微信
3,setTimeOut不自帶參數
①WEB
②微信IDE
③手機微信
三:結論
手機微信端是個報錯的坑 , 注意不要讓setTimeOut自帶參數.如下~~ this._timeout_id = setTimeout(
this.onTimeOutHasParams_2_bring.bind(this),
2000,
"Aonaufly",
18
);
其實在egret中沒有這個問題 , egret封裝了setTimeOut (egret.setTimeOut) .但是在LayaBox 和 Cocos Creator中就有這個問題.