优化前端防卡代码

This commit is contained in:
John Smith 2020-02-04 20:33:29 +08:00
parent b7e06652b4
commit 00e1f31f84

View File

@ -97,6 +97,14 @@ export default {
return this.atBottom/* || this.allowScroll*/ return this.atBottom/* || this.allowScroll*/
} }
}, },
watch: {
css(val) {
this.styleElement.innerText = val
},
canScrollToBottom(val) {
this.cantScrollStartTime = val ? null : new Date()
}
},
mounted() { mounted() {
this.styleElement.innerText = this.css this.styleElement.innerText = this.css
this.scrollToBottom() this.scrollToBottom()
@ -109,11 +117,6 @@ export default {
} }
this.clearMessages() this.clearMessages()
}, },
watch: {
css(val) {
this.styleElement.innerText = val
}
},
methods: { methods: {
addMessage(message) { addMessage(message) {
this.addMessages([message]) this.addMessages([message])
@ -446,29 +449,18 @@ export default {
scrollToBottom() { scrollToBottom() {
this.$refs.scroller.scrollTop = Math.pow(2, 24) this.$refs.scroller.scrollTop = Math.pow(2, 24)
this.atBottom = true this.atBottom = true
if (this.canScrollToBottom) {
this.cantScrollStartTime = null
}
}, },
onScroll() { onScroll() {
this.refreshCantScrollStartTime() this.refreshCantScrollStartTime()
let scroller = this.$refs.scroller let scroller = this.$refs.scroller
this.atBottom = scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight < SCROLLED_TO_BOTTOM_EPSILON this.atBottom = scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight < SCROLLED_TO_BOTTOM_EPSILON
if (this.canScrollToBottom) {
this.cantScrollStartTime = null
}
this.flushMessagesBuffer() this.flushMessagesBuffer()
}, },
canScrollToBottomOrTimedOut() { canScrollToBottomOrTimedOut() {
if (this.canScrollToBottom) { if (this.canScrollToBottom) {
this.cantScrollStartTime = null
return true return true
} }
// OBS // OBS
if (!this.cantScrollStartTime) {
this.cantScrollStartTime = new Date()
return false
}
return new Date() - this.cantScrollStartTime >= 10 * 1000 return new Date() - this.cantScrollStartTime >= 10 * 1000
}, },
refreshCantScrollStartTime() { refreshCantScrollStartTime() {