时间:2019-05-03 来源:小程序工厂
神***:
你要贴代码啊,光贴报错谁知道
赵***:
这个nodeId代码里面没有,应该是引入自定义后发生的问题
赵***:
Component({
/**
* 组件的属性列表
*/
properties: {
marqueeList: {
type: Array,
value: null,
observer: function (newVal) {
if (newVal instanceof Array && newVal.length > 0) {
this.initMarquee()
}
}
},
orientation: {
type: String,
value: 'left'
},
size: {
type: Number,
value: 28
}
},
/**
* 组件的初始数据
*/
data: {
content: '',
// content: '这是一条会滚动的文字滚来滚去的文字跑马灯,哈哈哈哈哈哈哈哈',
marqueePace: 2,//滚动速度
// marqueeDistance: 750,//初始滚动距离
interval: 20,// 时间间隔
count: 0
},
/**
* 组件的方法列表
*/
methods: {
run1: function () {
var that = this;
var timeout = setTimeout(function () {
if (that.data.marqueeDistance > -that.data.length) {//记住是right从0开始
that.setData({
marqueeDistance: that.data.marqueeDistance - that.data.marqueePace,
});
// that.data.marqueeDistance == 750 - (that.data.length - that.data.length % that.data.marqueePace)
if (that.data.marqueeDistance == 750 % that.data.marqueePace + 2 * that.data.marqueePace) {
setTimeout(function () {
that.run1();
}, that.data.marqueeList[that.data.count].showTime * 1000)
} else {
that.run1();
}
} else {
//一个回合结束,更新count
var count = that.data.count;
if (++count > that.data.marqueeList.length - 1) {
count = 0
}
that.setData({
count: count
})
that.initMarquee()
}
}, that.data.interval);
},
initMarquee: function () {
var that = this;
var marqueeData = that.data.marqueeList[that.data.count]
that.setData({
length: marqueeData.content.length * that.data.size,
marqueeDistance: 750,
content: marqueeData.content,
marqueePace: 750 * that.data.interval / 1000 / marqueeData.moveTime
});
that.run1();// 水平一行字滚动完了再按照原来的方向滚动
}
}
})
赵***:
{{content}}
赵***:
问题解决了,主要是返回其他页面时组件的setTimeout计时器还在运行就会出错,在组件的detached(组件被移除时调用的方法,相当于页面onUnload方法)方法中清除计时器就行了