Javascript 在Vue.js中使用watcher进行动画的Int-to-Int更改
我定期从事件侦听器接收每2000ms一个新值 一旦接收到新值,我想立即启动一个动画,从以前的值递增或递减到新值Javascript 在Vue.js中使用watcher进行动画的Int-to-Int更改,javascript,vue.js,Javascript,Vue.js,我定期从事件侦听器接收每2000ms一个新值 一旦接收到新值,我想立即启动一个动画,从以前的值递增或递减到新值 <script> import { animateIntToInt } from './helpers' export default { data () { return { speed: { currentvalue: 0 } } }, computed: { speed () => /
<script>
import { animateIntToInt } from './helpers'
export default {
data () {
return {
speed: {
currentvalue: 0
}
}
},
computed: {
speed () => // code to receive a speed value
},
watch: {
speed (to, from) {
this.speed = animateIntToInt(from.currentvalue, to.currentvalue, 1000)
}
}
}
</script>
每次我都会收到
未定义的值。我做错了什么?animateIntToInt
不会返回任何内容。真的吗?谢谢,我一点也没注意到…你正在你的速度观察设置速度。此外,速度是计算出来的,不应该手动设置。在收到新值之前,我需要以硬编码的方式更新此值。不使用查询选择方法重写动画函数以接受带速度的回调,然后更新速度属性。不需要计算机或监视程序。或者让它回报一个承诺。您的事件侦听器在哪里?
export function animateIntToInt (from, to, duration) {
let current = from
let difference = to - from
// Calculate the interval for loops
let interval = Math.abs(Math.floor(duration / difference))
// Determine direction to negative or positive
let step = to > from ? 1 : -1
let timer = setInterval(() => {
current += step
if (current === to) {
clearInterval(timer)
}
return current
}, interval)
}