安卓专用吐司toast倒计时到指定时间并显示剩余时间
安卓专用吐司toast倒计时到指定时间并显示剩余时间/**
*🍎交流 QQ 群 711841924 群一,苹果内测群,528816639
* 倒计时到指定时间并显示剩余时间(红色文字)
* @param {string} targetTime - 目标时间,格式为 'HH:mm:ss'
* 适用本文档ES5系统安卓 JavaScript引擎Rhino
*/
function countdownToTime(targetTime) {
// 设置文字颜色为红色
toast.setTextColor("#FF0000");
// 设置透明度 (可选)
toast.setAlpha(230);
// 设置字号 (可选)
toast.setSize(16);
// 解析目标时间
var now = new Date();
var target = new Date();
var timeParts = targetTime.split(':');
var hours = parseInt(timeParts);
var minutes = parseInt(timeParts);
var seconds = parseInt(timeParts) || 0;
target.setHours(hours, minutes, seconds, 0);
// 如果目标时间已过,则推迟到明天
if (target <= now) {
target.setDate(target.getDate() + 1);
}
// 计算初始剩余时间(秒)
var remainingSeconds = Math.floor((target - now) / 1000);
// 立即显示一次倒计时
showCountdown(remainingSeconds);
// 每秒更新一次倒计时
var interval = setInterval(function() {
remainingSeconds--;
showCountdown(remainingSeconds);
if (remainingSeconds <= 0) {
clearInterval(interval);
// 时间到了
toast.show("时间到了,开始执行任务!");
}
}, 1000);
return interval;
}
/**
* 显示倒计时信息(红色文字)
* @param {number} totalSeconds - 剩余总秒数
*/
function showCountdown(totalSeconds) {
// 确保文字颜色是红色
toast.setTextColor("#FF0000");
if (totalSeconds <= 0) {
toast.show("时间到了,开始执行任务!");
return;
}
var hours = Math.floor(totalSeconds / 3600);
var minutes = Math.floor((totalSeconds % 3600) / 60);
var seconds = totalSeconds % 60;
var message = "距离执行还剩: ";
if (hours > 0) {
message += hours + "小时 ";
}
if (minutes > 0) {
message += minutes + "分钟 ";
}
message += seconds + "秒";
toast.show(message);
}
// 使用示例
countdownToTime('18:50:00');
页:
[1]