HarmonyOS 鸿蒙Next广告页轮播图和倒计时不同步
HarmonyOS 鸿蒙Next广告页轮播图和倒计时不同步
移动app登录页面之前先进入广告页,广告页有三张图片进行轮播,每张图片展示5秒,每张图片展示够五秒后,下一张图片开始展示,倒计时从5秒开始。目前的问题是:进入广告页面后,广告已经展示出来,倒计时没有及时更新会延时一秒或两秒,导致倒计时还没结束,图片已经更换的情况。
2 回复
可参考如下Index.ets的代码:
import { router } from '@kit.ArkUI';
type KnownObject = Record<ESObject, ESObject>;
const CACHE_KEY = 'cachedAds'; // 定义缓存键
[@Entry](/user/Entry)
[@Component](/user/Component)
export default struct popupNews {
[@State](/user/State) dataSource: ESObject [] = [];
[@State](/user/State) countdown: number = 5 //倒计时
[@State](/user/State) isSkipped: string = ''
[@State](/user/State) fileNumber: string = ''
[@State](/user/State) startTime: string = ''
[@State](/user/State) endTime: string = ''
[@State](/user/State) filePictureUrl: string = ''
[@State](/user/State) resolution: string = '1200*2400'
[@State](/user/State) currentImageIndex: number = 0; // 当前展示的图片索引
[@State](/user/State) filePictureUrls: string[] = [
// "http://47.115.226.142:4433/api/oss/file/download/zipbucketname/9155399915459.png",
"https://so2.360tres.com/dmfd/400_300_/t01b441f337f9f46aa4.jpg",
// "http://47.115.226.142:4433/api/oss/file/download/zipbucketname/5429693342363.png",
"https://wxls-cms.oss-cn-hangzhou.aliyuncs.com/online/2024-04-18/218da022-f4bf-456a-99af-5cb8e157f7b8.jpg",
// "http://47.115.226.142:4433/api/oss/file/download/zipbucketname/7407571805368.png"
"https://img1.baidu.com/it/u=465289696,3068910287&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067"
]; // 存储图片URLs的数组
[@State](/user/State) currentCountdownId: number = 5 //保存当前倒计时ID的属性
[@State](/user/State) isCountdownFinished: boolean = false;
private swiperController: SwiperController = new SwiperController() as ESObject
onPageShow() {
this.jumpToLoginPageAfterTimeout()
}
jumpToLoginPageAfterTimeout() {
setTimeout(() => {
if (!this.isCountdownFinished) {
this.goToLoginPage();
}
}, 15000);
}
// 新增方法,跳转到登录页面
goToLoginPage() {
// 清除轮播定时器
// if (this.slideshowTimer) {
// clearTimeout(this.slideshowTimer);
// this.slideshowTimer = null;
// }
// 跳转到登录页面的逻辑
router.replaceUrl({
url: 'pages/Login',
})
}
// 新增方法,启动倒计时
startCountdown(seconds: number, index: number) {
if (this.currentCountdownId !== null) {
clearInterval(this.currentCountdownId);
}
let remainingTime = seconds;
this.countdown = remainingTime
this.currentCountdownId = setInterval(() => {
remainingTime--;
// if (remainingTime === 0) {
// clearInterval(this.currentCountdownId); // 倒计时结束,清除定时器
// this.isCountdownFinished = true; // 设置标志位
// this.goToLoginPage(); // 跳转到登录页面
// } else {
// this.countdown = remainingTime; // 更新倒计时状态
// remainingTime--; // 倒计时递减
// }
//图片轮播到最后一张
if (index === this.filePictureUrls.length - 1) {
this.countdown = remainingTime
//读秒结束
if (remainingTime === 0) {
clearInterval(this.currentCountdownId); // 倒计时结束,清除定时器
this.isCountdownFinished = true; // 设置标志位
this.goToLoginPage(); // 跳转到登录页面
}
} else {
this.countdown = remainingTime
if (remainingTime === 0) {
this.swiperController.showNext()
}
}
}
, 1000
)
;
}
// startCountdown(index:number) {
// const timerId = setInterval(() => {
// if (this.countdown === 0) {
// this.goToLoginPage();
// clearInterval(timerId);
// } else {
// this.currentCountdownId = this.currentCountdownId -1 // 更新倒计时字符串
// this.countdown = this.countdown -1
// }
// }, 1000);
// // 保存当前倒计时的ID,以便后续可以清除
// this.currentCountdownId = this.currentCountdownId;
// }
// 新增方法,处理跳过按钮的点击事件
skipAd() {
if (this.currentCountdownId) {
clearInterval(this.countdown)
clearInterval(this.currentCountdownId); //清除当前计时器
}
this.isCountdownFinished = true; // 设置标志位
this.goToLoginPage(); // 跳转到登录页面
}
//广告弹窗配置
// getBannerList(resolution: string) {
// try{
// getBannerListData(`/openapi/portal/ad/list?resolution=`+ resolution)
// .then((data: ESObject) => {
// if (data.code === 2000) {
// this.dataSource = data.data.body
// // this.countdown = data.data.body.countdown;
// this.fileNumber = data.data.body.fileNumber;
// this.startTime = data.data.body.startTime;
// this.endTime = data.data.body.endTime;
// this.filePictureUrl = data.data.body.fileInfo.filePictureUrl;
// this.filePictureUrls =data.data.body.fileInfo.filePictureUrl.split(',');
// this.startCountdown(5);
// }
// })
// .catch((err: Error) => {
// showToast($r('app.string.http_error_message'));
// });
// }catch (error) {
// showToast($r('app.string.http_error_message'));
// }
// }
build() {
Row() {
Stack() {
Swiper(this.swiperController) {
ForEach(this.filePictureUrls, (item: ESObject, index) => {
Stack() {
Image(item)
.width('100%')
.height('100%')
.onComplete(() => {
console.log("edawefdawefawefaefcawef")
this.startCountdown(5, index);
})
Button('跳过' + this.countdown + 's')
}.width('100%')
}, (item: ESObject) => item)
}
.interval(5000)
.loop(true)
.width('100%')
// .autoPlay(true)
.borderRadius(6)
.onChange((index: number) => {
// 当滑动到新图片时,重置倒计时为5秒
this.startCountdown(5, index);
this.currentImageIndex = index
})
Column() {
Button('跳过' + this.countdown + 's')
.onClick(() => {
this.skipAd(); // 点击跳过按钮时调用跳过逻辑
})
.width(75)
.height(30)
.backgroundColor('#D3D3D3')
.fontSize(11)// 设置字体大小
.fontWeight(80)
}.align(Alignment.TopEnd)
.margin({ top: -720, right: -260 })
}
}.width('100%')
.height('100%')
}
} 更多关于HarmonyOS 鸿蒙Next广告页轮播图和倒计时不同步的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对HarmonyOS 鸿蒙Next广告页轮播图和倒计时不同步的问题,可以尝试以下解决方案:
- 检查数据同步逻辑:确保轮播图和倒计时的数据同步逻辑正确,避免数据更新延迟或不一致。使用高效的同步机制,如事件监听或数据绑定,确保两者状态实时同步。
- 优化计时器实现:检查倒计时实现方式,确保计时器准确且稳定。使用系统提供的计时器组件,并合理设置计时器回调,以便在倒计时结束时更新UI。
- UI更新策略:确保UI更新在正确的线程进行,避免主线程阻塞。对于轮播图和倒计时的更新,应使用异步操作或后台线程处理,然后通过Handler或类似机制更新主线程UI。
- 资源管理:检查系统资源使用情况,避免因资源不足导致更新延迟。优化应用性能,减少不必要的资源消耗。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html 。

