由于WebAssembly运行时没有提供定时器API,所以Waft封装了对定时器的支持。
setTimeout(audio: DataCallback, delay: i64, target: Targert | null)
设置定时器
import { setTimeout, console } from 'waft';
setTimeout(()=>{
console.log("timer fire")
},3000)
clearTimeout(timerId: i64)
清除定时器
import { setTimeout, clearTimeout, console } from 'waft';
let timerId = setTimeout(()=>{
console.log("timer fire")
},3000)
clearTimeout(timerId);
setInterval(audio: DataCallback, delay: i64, target: Targert | null)
import { setInterval, console } from 'waft';
setInterval(()=>{
console.log("timer fire")
},1000)
clearInterval(timerId: i64)
清除定时器
import { setTimeout, clearTimeout, console } from 'waft';
let timerId = setInterval(()=>{
console.log("timer fire")
}, 1000)
clearInterval(timerId);
如何在回调的闭包里透传数据
this可以直接透传
import { Target } from "waft";
export class Index extends Page {
constructor(props: Props){
super(props);
}
onLoad(query: JSONObject): void{
// 页面加载后
console.log('page onLoad:' + JSON.stringify(query));
// setTimeout是前端框架自定义提供的函数
// 第三个参数,可以自定义传入透传(必须继承于Target)
// 后会通过回调的第二个参数返回过来
setTimeout((_, target)=>{
console.log("timer fire");
const _this = (target as Index);
// _this.setState()
},3000, this)
}
}
复杂数据建议用class包裹
import { Target } from "waft";
class DataBundle extends Target{
dataString: string = "";
}
const bundle = new DataBundle();
bundle.dataString = "xxxxxxxx";
// setTimeout是前端框架自定义提供的函数
// 第三个参数 bunlde 可以自定义传入透传(必须继承于Target)
// 后会通过回调的第二个参数bundle返回过来
setTimeout((_, bundle)=>{
console.log("timer fire");
const dataString = (bundle as DataBundle).dataString;
},3000, bundle)