阅读(496) (10)

百度智能小程序 取消setInterval定时器

2020-08-13 15:28:21 更新

clearInterval

解释:取消由 setInterval 设置的定时器。

方法参数

Number intervalID

intervalID参数说明 :要取消的定时器的 ID。

示例

在开发者工具中打开


图片示例


代码示例

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">
            <view>Interval</view>
            <view>{{Interval}}</view>
        </view>
        <button type="primary" bindtap="bindSetInterval">setInterval</button>
        <button type="primary" bindtap="bindClearInterval">clearInterval</button>
    </view>
    <view class="card-area">
        <view class="top-description border-bottom">
            <view>timeout</view>
            <view>{{timeout}}</view>
        </view>
        <button type="primary" bindtap="bindSetTimeout">setTimeout</button>
        <button type="primary" bindtap="bindClearTimeout">clearTimeout</button>
    </view>
</view>
Page({
    data: {
        Interval: 0,
        timeout: 0
    },
    bindSetTimeout() {
        this.timeout && clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
            swan.showToast({
                title: 'timeout',
                icon: 'none'
            });
            this.setData({
                timeout: 3
            })
        }, 3000);
    },
    bindClearTimeout() {
        this.timeout && clearTimeout(this.timeout);
        this.setData({
            timeout: 0
        })
    },
    bindSetInterval() {
        this.interval && clearInterval(this.interval);
        this.interval = setInterval(() => {
            swan.showToast({
                title: 'setInterval',
                icon: 'none'
            });
            this.data.Interval += 3;
            this.setData({
                Interval: this.data.Interval
            })
        }, 3000);
    },
    bindClearInterval() {
        this.interval && clearInterval(this.interval);
        this.setData({
            Interval: 0
        })
    }
});