想在不同的方法中添加clearInterval怎么办?
What should I do if I want to add clearInterval in diferent methods?
我使用 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)
},
在 moveMarker
方法上调用 clearInterval
不会执行任何操作。您需要将间隔 ID 保存在 stop
可以访问的地方。
例如里面 moveMarker
:
this.intervalId = loop
然后:
stop () {
clearInterval(this.intervalId)
}
无需将 intervalId
添加到您的 data
,因为您不需要它来响应。
首先,您应该将间隔存储在数据 属性 中,以便在 stop
方法中访问它。然后在 stop
方法中调用 clearInterval
并存储 interval
:
export default {
...
data() {
return {
interval: null
}
},
methods: {
moveMarker () {
const moveCoordinate = []
this.interval = setInterval(() => {
if (this.index + 1 === (this.coordinates.length - 1)) {
clearInterval(this.interval)
}
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.interval)
},
},
...
}
我使用 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)
},
在 moveMarker
方法上调用 clearInterval
不会执行任何操作。您需要将间隔 ID 保存在 stop
可以访问的地方。
例如里面 moveMarker
:
this.intervalId = loop
然后:
stop () {
clearInterval(this.intervalId)
}
无需将 intervalId
添加到您的 data
,因为您不需要它来响应。
首先,您应该将间隔存储在数据 属性 中,以便在 stop
方法中访问它。然后在 stop
方法中调用 clearInterval
并存储 interval
:
export default {
...
data() {
return {
interval: null
}
},
methods: {
moveMarker () {
const moveCoordinate = []
this.interval = setInterval(() => {
if (this.index + 1 === (this.coordinates.length - 1)) {
clearInterval(this.interval)
}
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.interval)
},
},
...
}