// components/background/snow/index.ts import Toolkit from "../../../utils/Toolkit"; interface Snowflake { x: number; s: number; speed: number; } Component({ data: { snowflakes: [] as Snowflake[], timer: undefined as number | undefined, docWidth: 375, docHeight: 667, }, methods: { createSnowflake() { const snowflake = { x: Toolkit.random(0, this.data.docWidth), s: Toolkit.random(6, 20), speed: Toolkit.random(14, 26) }; this.setData({ snowflakes: [...this.data.snowflakes, snowflake] }); } }, lifetimes: { attached() { const systemInfo = wx.getWindowInfo(); this.setData({ docWidth: systemInfo.windowWidth, docHeight: systemInfo.windowHeight, timer: setInterval(() => { this.createSnowflake(); console.log(this.data.snowflakes); if (40 < this.data.snowflakes.length) { if (this.data.timer) { clearInterval(this.data.timer); } this.setData({ timer: undefined }) } }, 2000) }); }, detached() { if (this.data.timer) { clearInterval(this.data.timer); this.setData({ timer: undefined }) } } } })