| 1234567891011121314151617181920212223242526272829303132333435 |
- /**
- * 试看时长计量(秒);-1 表示免费全场
- */
- export class TrialMeter {
- constructor(onExpire) {
- this.onExpire = onExpire
- this.secondsLeft = null
- this._timer = null
- }
- start(seconds) {
- this.stop()
- this.secondsLeft = seconds
- if (seconds === -1 || seconds === null || seconds === undefined) return
- if (seconds <= 0) {
- this.onExpire?.()
- return
- }
- this._timer = setInterval(() => {
- if (this.secondsLeft <= 0) return
- this.secondsLeft -= 1
- if (this.secondsLeft <= 0) {
- this.stop()
- this.onExpire?.()
- }
- }, 1000)
- }
- stop() {
- if (this._timer) {
- clearInterval(this._timer)
- this._timer = null
- }
- }
- }
|