倒计时(原生写法)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>倒计时</title>
    <style type="text/css">
        html,body,div,span{
            font-size: 16px;
            font-family: "微软雅黑";
        }
        #div1{
            width: 600px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            margin:50px auto;
            background: -moz-linear-gradient(left, red, #f96, yellow, green, #ace);
            background: -webkit-linear-gradient(left,red,#f96,yellow,green,#ace);
            background: -o-linear-gradient(left, red, #f96, yellow, green, #ace);
        }
        #date1,#hour1,#minute1,#second1{
            color: red;
            font-weight: bold;
        }
        #date2,#hour2,#minute2,#second2{
            color:#c49d62;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div id="div1">
    <span>距离您中奖还有</span>
    <span id="date1"></span><span id="date2"></span><span>天</span>
    <span id="hour1"></span><span id="hour2"></span><span>时</span>
    <span id="minute1"></span><span id="minute2"></span><span>分</span>
    <span id="second1"></span><span id="second2"></span><span>秒</span>
</div>
    <script type="text/javascript">
    // 注:这里用到的当前时间为本机电脑时间,如果是项目需要,请获取服务器当前时间来做计算部分
        function addZero(num){
            //此方法为处理一位数字的时候添零补齐两位
            return num<10?"0"+num:num;
        }
        function remind(){
            var str="2015-7-26 13:53:00";
            str=str.replace(/[-]/g,"/");
            var oDate1=document.getElementById('date1');
            var oDate2=document.getElementById('date2');
            var oHour1=document.getElementById('hour1');
            var oHour2=document.getElementById('hour2');
            var oMinute1=document.getElementById('minute1');
            var oMinute2=document.getElementById('minute2');
            var oSecond1=document.getElementById('second1');
            var oSecond2=document.getElementById('second2');
            //注:以上为获取元素,以下为计算时间
            var spanTimeNow =new Date().getTime();//当前时间距离1970年1月1日午夜之间的毫秒数
            var spanTimeTarget= Date.parse(str);//目标时间距离1970年1月1日午夜之间的毫秒数
            var spanTime=spanTimeTarget-spanTimeNow;
            var date=addZero(Math.floor(spanTime/(1000*60*60*24)));
            var hour=addZero(Math.floor(spanTime/(1000*60*60)-date*24));
            var minute=addZero(Math.floor(spanTime/(1000*60)-date*24*60-hour*60));
            var second=addZero(Math.floor(spanTime/1000-date*24*60*60-hour*60*60-minute*60));
            //注:将获得的时间添加到指定位置
            oDate1.innerHTML=date.toString().substr(0,1);
            oDate2.innerHTML=date.toString().substr(1,1);
            oHour1.innerHTML=hour.toString().substr(0,1);
            oHour2.innerHTML=hour.toString().substr(1,1);
            oMinute1.innerHTML=minute.toString().substr(0,1);
            oMinute2.innerHTML=minute.toString().substr(1,1);
            oSecond1.innerHTML=second.toString().substr(0,1);
            oSecond2.innerHTML=second.toString().substr(1,1);
        }
        // window.setInterval(function(){
        //     remind();
        // },6000)
        window.setInterval(remind,1000);//只有在remind里面没有参数
    </script>
</body>
</html>
import router from '@ohos.router'; import { notificationManager } from '@kit.NotificationKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; @Entry @Component struct Index { @State inputText: string = ''; private notificationId: number = 1; // 普通文本通知 private publishBasicTextNotification() { if (!this.inputText) { AlertDialog.show({ message: '请输入通知内容', autoCancel: true }); return; } let notificationRequest: notificationManager.NotificationRequest = { id: this.notificationId++, content: { notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: '普通文本通知', text: this.inputText, additionalText: '来自鸿蒙应用' } } }; notificationManager.publish(notificationRequest, (err: BusinessError) => { if (err) { hilog.error(0xFF00, '[NotificationDemo]', `发布失败: ${err.message}`); AlertDialog.show({ message: '通知发布失败', autoCancel: true }); } else { hilog.info(0xFF00, '[NotificationDemo]', '普通文本通知发布成功'); AlertDialog.show({ message: '通知发布成功', autoCancel: true }); } }); } // 多行文本通知 private publishMultiLineNotification() { let notificationRequest: notificationManager.NotificationRequest = { id: this.notificationId++, content: { notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, multiLine: { title: '多行文本通知', text: '这是一个多行文本通知示例', briefText: '多行内容概要', longTitle: '展开后的长标题', lines: [ '第一行内容', '第二行内容', '第三行内容', '第四行内容' ] } } }; notificationManager.publish(notificationRequest, (err: BusinessError) => { if (err) { hilog.error(0xFF00, '[NotificationDemo]', `发布失败: ${err.message}`); AlertDialog.show({ message: '通知发布失败', autoCancel: true }); } else { hilog.info(0xFF00, '[NotificationDemo]', '多行文本通知发布成功'); AlertDialog.show({ message: '通知发布成功', autoCancel: true }); } }); } // 倒计时提醒 private publishCountdownNotification() { let countdown: number = 10; let notificationId = this.notificationId++; // 更新倒计时的函数 const updateCountdown = () => { let notificationRequest: notificationManager.NotificationRequest = { id: notificationId, content: { notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: '倒计时提醒', text: `剩余时间: ${countdown}秒`, additionalText: '10秒倒计时结束提醒' } } }; notificationManager.publish(notificationRequest, (err: BusinessError) => { if (err) { hilog.error(0xFF00, '[NotificationDemo]', `更新失败: ${err.message}`); } else { hilog.info(0xFF00, '[NotificationDemo]', `倒计时更新: ${countdown}秒`); } }); }; // 初始发布 updateCountdown(); // 设置定时器更新倒计时 const timer = setInterval(() => { countdown--; if (countdown >= 0) { updateCountdown(); } else { clearInterval(timer); // 倒计时结束,发布完成通知 let finishRequest: notificationManager.NotificationRequest = { id: notificationId, content: { notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: '倒计时提醒', text: '10秒倒计时已结束', additionalText: '任务完成' } } }; notificationManager.publish(finishRequest, (err: BusinessError) => { if (err) { hilog.error(0xFF00, '[NotificationDemo]', `结束通知失败: ${err.message}`); } else { hilog.info(0xFF00, '[NotificationDemo]', '倒计时结束通知发布成功'); } }); } }, 1000); } build() { Column() { Text('通知功能演示') .fontSize(24) .fontWeight(FontWeight.Bold) .margin({ top: 50, bottom: 30 }) // 使用ArkTS原生TextInput组件 // 修正后的 TextInput 使用方式 TextInput({ placeholder: '请输入通知内容' }) .onChange((value: string) => { this.inputText = value; }) .type(InputType.Normal) // 使用链式调用设置类型 .width('90%') .margin({ bottom: 20 }) .backgroundColor('#F5F5F5') .borderRadius(8) .padding(10) Button('发布普通文本通知') .width('90%') .margin({ bottom: 15 }) .onClick(() => { this.publishBasicTextNotification(); }) Button('发布多行文本通知') .width('90%') .margin({ bottom: 15 }) .onClick(() => { this.publishMultiLineNotification(); }) Button('发布10秒倒计时提醒') .width('90%') .onClick(() => { this.publishCountdownNotification(); }) } .width('100%') .height('100%') .padding(20) } }为什么发布通知都失败了,而且十秒那个还没有反应,请你仔细排查错误并帮我改正
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值