本文目录导读:

- 浏览器端(JavaScript)
- Node.js 桌面脚本(配合 electron 或 node-notifier)
- Python 脚本
- 批处理脚本(Windows)
- Shell 脚本(macOS/Linux)
- HTML 页面示例(完整)
- 选择建议
浏览器端(JavaScript)
// 简单的定时弹窗(使用alert,会阻塞操作)
function scheduleAlert(message, minutes) {
setTimeout(() => {
alert(message);
}, minutes * 60 * 1000);
}
// 使用示例:10分钟后提醒
scheduleAlert('该休息一下了!', 10);
// 更友好的通知(使用Notification API)
function scheduleNotification(message, minutes) {
if (!("Notification" in window)) {
alert('您的浏览器不支持桌面通知');
return;
}
// 请求权限
Notification.requestPermission().then(permission => {
if (permission === "granted") {
setTimeout(() => {
new Notification('提醒', {
body: message,
icon: 'https://via.placeholder.com/48'
});
}, minutes * 60 * 1000);
}
});
}
// 使用示例:30分钟后提醒
scheduleNotification('记得喝水!', 30);
Node.js 桌面脚本(配合 electron 或 node-notifier)
// 首先安装:npm install node-notifier
const notifier = require('node-notifier');
const path = require('path');
function scheduleNotification(message, minutes) {
setTimeout(() => {
notifier.notify({
title: '提醒',
message: message,
icon: path.join(__dirname, 'icon.png'), // 可选图标
sound: true,
wait: false
});
}, minutes * 60 * 1000);
console.log(`提醒已设置,将在 ${minutes} 分钟后显示`);
}
// 使用示例
scheduleNotification('检查邮件', 15);
Python 脚本
import time
import tkinter as tk
from tkinter import messagebox
def schedule_popup(message, minutes):
"""定时弹窗提醒"""
seconds = minutes * 60
time.sleep(seconds)
# 创建临时窗口来显示弹窗
root = tk.Tk()
root.withdraw() # 隐藏主窗口
messagebox.showinfo("提醒", message)
root.destroy()
# 使用示例
# schedule_popup("该休息了!", 5) # 5分钟后弹窗
# 更多功能版本:支持循环提醒
def repeated_reminder(message, interval_minutes, times):
"""循环提醒多次"""
for i in range(times):
schedule_popup(f"{message} (第{i+1}次)", interval_minutes)
time.sleep(interval_minutes * 60)
# 每30分钟提醒一次,共3次
# repeated_reminder("站起来活动一下", 30, 3)
批处理脚本(Windows)
@echo off
:: Windows 定时弹窗提醒脚本
:loop
:: 设置等待时间(秒)
timeout /t 1800 /nobreak >nul
:: 弹出提醒窗口
msg %username% "该休息一下了!"
:: 或者用更现代的提示
powershell -Command "& {Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('该休息一下了!', '提醒')}"
:: 无限循环
goto loop
Shell 脚本(macOS/Linux)
#!/bin/bash
# macOS 使用 osascript
remind_macos() {
osascript -e "display dialog \"$1\" buttons {\"确定\"} default button 1 with title \"提醒\""
}
# Linux 使用 notify-send (需要安装libnotify-bin)
remind_linux() {
notify-send "提醒" "$1"
}
# 定时执行
schedule_reminder() {
local message="$1"
local minutes=$2
sleep $((minutes * 60))
if [[ "$OSTYPE" == "darwin"* ]]; then
remind_macos "$message"
else
remind_linux "$message"
fi
}
# 使用示例
schedule_reminder "记得喝水!" 30
HTML 页面示例(完整)
<!DOCTYPE html>
<html>
<head>定时提醒工具</title>
<style>
body { font-family: Arial; padding: 20px; }
input, button { margin: 5px; padding: 8px; }
</style>
</head>
<body>
<h2>定时提醒设置</h2>
<label>提醒内容:<input type="text" id="message" value="该休息了"></label><br>
<label>分钟数:<input type="number" id="minutes" value="5" min="1"></label><br>
<button onclick="startReminder()">开始计时</button>
<button onclick="cancelReminder()">取消提醒</button>
<script>
let timerId = null;
function startReminder() {
const message = document.getElementById('message').value;
const minutes = parseInt(document.getElementById('minutes').value);
if (timerId) {
clearTimeout(timerId);
}
timerId = setTimeout(() => {
alert(message);
timerId = null;
}, minutes * 60 * 1000);
alert(`提醒已设置,将在 ${minutes} 分钟后显示`);
}
function cancelReminder() {
if (timerId) {
clearTimeout(timerId);
timerId = null;
alert('提醒已取消');
} else {
alert('没有活动的提醒');
}
}
</script>
</body>
</html>
选择建议
| 场景 | 推荐方案 |
|---|---|
| 浏览器使用 | JavaScript + Notification API |
| 桌面应用(跨平台) | Node.js + node-notifier |
| 简单脚本(Windows) | Batch 脚本 |
| 简单脚本(macOS/Linux) | Shell 脚本 |
| 企业环境/高级需求 | Python + tkinter |
需要我帮您调整某个具体场景的实现吗?