From 3d99f6e9ed3038383febf26bde446eb87c63d90b Mon Sep 17 00:00:00 2001
From: Mimi <1119186082@qq.com>
Date: Sun, 29 Dec 2019 23:39:46 +0800
Subject: [PATCH] Refactor showHitokoto
---
waifu-tips.js | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/waifu-tips.js b/waifu-tips.js
index 56c9a40..5bfe3cd 100755
--- a/waifu-tips.js
+++ b/waifu-tips.js
@@ -92,9 +92,9 @@ function loadWidget(waifuPath, apiPath) {
showMessage(text, 7000, 8);
}
welcomeMessage();
- // 检测用户活动状态,并在空闲时定时显示一言
+ // 检测用户活动状态,并在空闲时显示消息
var userAction = false,
- hitokotoTimer = null,
+ userActionTimer = null,
messageTimer = null,
messageArray = ["好久不见,日子过得好快呢……", "大坏蛋!你都多久没理人家了呀,嘤嘤嘤~", "嗨~快来逗我玩吧!", "拿小拳拳锤你胸口!"];
if ($(".fa-share-alt").is(":hidden")) messageArray.push("记得把小家加入 Adblock 白名单哦!");
@@ -104,19 +104,20 @@ function loadWidget(waifuPath, apiPath) {
userAction = true;
});
setInterval(() => {
- if (!userAction) {
- if (!hitokotoTimer) hitokotoTimer = setInterval(showHitokoto, 25000);
- } else {
+ if (userAction) {
userAction = false;
- clearInterval(hitokotoTimer);
- hitokotoTimer = null;
+ clearInterval(userActionTimer);
+ userActionTimer = null;
+ } else if (!userActionTimer) {
+ userActionTimer = setInterval(() => {
+ showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9);
+ }, 20000);
}
}, 1000);
function showHitokoto() {
// 增加 hitokoto.cn 的 API
- if (Math.random() < 0.6 && messageArray.length > 0) showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9);
- else fetch("https://v1.hitokoto.cn")
+ fetch("https://v1.hitokoto.cn")
.then(response => response.json())
.then(result => {
var text = `这句一言来自 「${result.from}」,是 ${result.creator} 在 hitokoto.cn 投稿的。`;