百度智能小程序 取消setInterval定时器
2020-09-05 11:02 更新
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
})
}
});
以上内容是否对您有帮助:
更多建议: