【发布时间】:2019-12-19 16:18:32
【问题描述】:
我有moveMarker 和setInterval 的方法来使我在mapbox-gl 中的标记沿着我的路线线移动,并且我已经播放按钮来触发这个功能。我的问题是,如果我想用 clearInterval 创建暂停按钮该怎么办?
我尝试在moveMarker 中创建clearInterval 的函数,但不起作用
这是我移动标记的功能:
moveMarker () {
const moveCoordinate = []
const loop = setInterval(() => {
if (this.index + 1 === (this.coordinates.length - 1)) {
clearInterval(loop)
}
for (let index = 0; index < moveCoordinate.length; index++) {
moveCoordinate[index].remove()
}
this.map.panTo(this.coordinates[this.index])
const lat = this.coordinates[this.index][0]
const lng = this.coordinates[this.index][1]
const newMarker = new mapboxgl.LngLat(lat, lng)
console.log('new', newMarker)
const markerMapbox = new mapboxgl.Marker()
.setLngLat(newMarker)
.addTo(this.map)
moveCoordinate.push(markerMapbox)
this.index++
}, 1000)
},
这是停止功能:
stop () {
clearInterval(this.moveMarker)
},
【问题讨论】:
标签: node.js vue.js setinterval nuxt.js clearinterval