Commit f85df08a by 王志慧

commit:定时器优化

1 parent 4ab02ebe
......@@ -57,18 +57,18 @@ export default {
},
computedValue() {
const { marqueeBox } = this.$refs
console.log(this.text)
// console.log(this.text)
this.boxWidth = marqueeBox.offsetWidth // 盒子宽度
this.valueWidth = this.text.visualLength(this.fontSize) // 内容文本宽度
const { boxWidth, valueWidth } = this
console.log(valueWidth, boxWidth)
// console.log(valueWidth, boxWidth)
if (valueWidth < boxWidth) {
this.$set(this, 'speed', 0)
} else {
this.$set(this, 'speed', this.scrollamount)
}
this.key = new Date().getTime()
console.log(valueWidth > boxWidth, this.speed, this.scrollamount)
// console.log(valueWidth > boxWidth, this.speed, this.scrollamount)
}
}
}
......
......@@ -51,7 +51,6 @@ export default {
return {
option: '',
echartsPie: '',
mTime: '',
viewData: [],
text: '',
subtext: '',
......@@ -76,6 +75,7 @@ export default {
},
mounted() {
console.log(this.dep)
// this.initData()
// this.draw()
},
......@@ -93,7 +93,7 @@ export default {
})
},
draw() {
if (this.mTime) clearInterval(this.mTime)
this.dep.remove(this.id)
this.echartsPie = echarts.init(document.getElementById(this.id))
window.addEventListener('resize', () => {
this.echartsPie.resize()
......@@ -142,11 +142,11 @@ export default {
// this.setAnamiteText()
this.echartsPie.setOption(this.option)
this.pieActive()
this.mTime = setInterval(this.pieActive, 15000)
this.dep.addSub({ id: this.id, fn: this.pieActive })
this.dep.notify()
// 鼠标划出
this.echartsPie.on('mouseout', () => {
if (this.mTime) clearInterval(this.mTime)
this.mTime = setInterval(this.pieActive, 15000)
this.dep.addSub({ id: this.id, fn: this.pieActive })
})
this.echartsPie.on('mouseover', data => {
this.mainValue = data.data.value
......@@ -154,7 +154,8 @@ export default {
this.descColor = data.color
this.setAnamiteText()
// 停止定时器,清除之前的高亮
clearInterval(this.mTime)
// clearInterval(this.mTime)
this.dep.remove(this.id)
this.currentIndex = data.dataIndex
this.highlightPie()
this.emitEvent(data.data)
......
......@@ -20,6 +20,7 @@ import { fetchConfig, cfg } from '@/config'
import '@/assets/css/index.scss'
import './utils/util'
import 'components'
import Dep from './utils/dep'
Vue.prototype.to = function (name, params) {
if (name === -1) {
router.go(-1)
......@@ -29,11 +30,10 @@ Vue.prototype.to = function (name, params) {
}
// 移动端适配 rem
// import 'lib-flexible/flexible.js'
Vue.prototype.dep = new Dep()
Vue.config.productionTip = false
const isMock = ['prod','production'].includes(process.env.NODE_ENV)
fetchConfig(!isMock).then(res => {
console.log(res)
// 从配置服务器获取动态配置
// 设置 js中可以访问 $cdn
Vue.use(cfg)
......
export default class Dep {
constructor() {
this.subs = [] // 此数组用来收集订阅
this.mTime = ''
}
//事件收集
addSub(data) {
let isAdd = true
this.subs.forEach(sub => {
if (sub.id == data.id) {
isAdd = false
sub.fn = data.fn
}
})
if (isAdd) this.subs.push(data)
}
remove(id) {
this.subs.forEach(sub => {
if (sub.id == id) sub.fn = ''
})
}
//事件触发
notify() {
if (this.mTime) clearInterval(this.mTime)
this.mTime = setInterval(() => {
this.subs.forEach(sub => {
sub.fn && sub.fn()
})
}, 15000)
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!