自己无聊 准备自己写一个老虎机来玩 要用关键帧来啥,开始了那肯定要结束
不想看案例的,最底部有错误原因!!!
错误写法
const state = reactive({
render: 0
})
const render = () => {
const curIdx = state.idx[state.idx.length - 1]
if (curIdx === 12) {
cancelAnimationFrame(state.render)
}
if (state.circle > 5) {
if (curIdx === state.resultRenderIdx) {
state.isEnd = true
cancelAnimationFrame(state.render)
renderLastAnimate()
}
}
renderIdxHook()
state.render = requestAnimationFrame(render)
}
正确写法
const state = reactive({
render: 0,
isEnd: false
})
const render = () => {
const curIdx = state.idx[state.idx.length - 1]
if (curIdx === 12) {
cancelAnimationFrame(state.render)
}
if (state.circle > 5) {
if (curIdx === state.resultRenderIdx) {
state.isEnd = true
}
}
renderIdxHook()
state.render = requestAnimationFrame(render)
}
watchEffect(() => {
if (state.isEnd === true) {
cancelAnimationFrame(state.render)
renderLastAnimate()
}
})
总结
错误写法:将cancelAnimationFrame这个方法写在我们执行的关键帧里——失败
正确写法:将cancelAnimationFrame这个方法写在我们执行的关键帧外——成功
|