Commit f85df08a by 王志慧

commit:定时器优化

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