由于WebAssembly运行时没有提供定时器API,所以Waft封装了对定时器的支持。

setTimeout(audio: DataCallback, delay: i64, target: Targert | null)

设置定时器

  1. import { setTimeout, console } from 'waft';
  2. setTimeout(()=>{
  3. console.log("timer fire")
  4. },3000)


clearTimeout(timerId: i64)

清除定时器

  1. import { setTimeout, clearTimeout, console } from 'waft';
  2. let timerId = setTimeout(()=>{
  3. console.log("timer fire")
  4. },3000)
  5. clearTimeout(timerId);


setInterval(audio: DataCallback, delay: i64, target: Targert | null)

  1. import { setInterval, console } from 'waft';
  2. setInterval(()=>{
  3. console.log("timer fire")
  4. },1000)


clearInterval(timerId: i64)

清除定时器

  1. import { setTimeout, clearTimeout, console } from 'waft';
  2. let timerId = setInterval(()=>{
  3. console.log("timer fire")
  4. }, 1000)
  5. clearInterval(timerId);

如何在回调的闭包里透传数据

this可以直接透传

  1. import { Target } from "waft";
  2. export class Index extends Page {
  3. constructor(props: Props){
  4. super(props);
  5. }
  6. onLoad(query: JSONObject): void{
  7. // 页面加载后
  8. console.log('page onLoad:' + JSON.stringify(query));
  9. // setTimeout是前端框架自定义提供的函数
  10. // 第三个参数,可以自定义传入透传(必须继承于Target
  11. // 后会通过回调的第二个参数返回过来
  12. setTimeout((_, target)=>{
  13. console.log("timer fire");
  14. const _this = (target as Index);
  15. // _this.setState()
  16. },3000, this)
  17. }
  18. }

复杂数据建议用class包裹

  1. import { Target } from "waft";
  2. class DataBundle extends Target{
  3. dataString: string = "";
  4. }
  5. const bundle = new DataBundle();
  6. bundle.dataString = "xxxxxxxx";
  7. // setTimeout是前端框架自定义提供的函数
  8. // 第三个参数 bunlde 可以自定义传入透传(必须继承于Target
  9. // 后会通过回调的第二个参数bundle返回过来
  10. setTimeout((_, bundle)=>{
  11. console.log("timer fire");
  12. const dataString = (bundle as DataBundle).dataString;
  13. },3000, bundle)