Skip to content

Commit 5d7515b

Browse files
committed
在页面中埋藏一定量炸药
1 parent fab4fe4 commit 5d7515b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/layouts/BaseLayout.astro

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,53 @@ const { description, keywords, title } = Astro.props;
7676
{ passive: true }
7777
);
7878
}
79+
80+
const title = document.title;
81+
82+
let seconds = 10;
83+
let timer: NodeJS.Timer | null;
84+
let isPaused = true;
85+
let countdown = `${seconds}`;
86+
const boom = Math.random() < 0.8 ? "爆炸了!!!" : "熄火了...";
87+
88+
const start = () => {
89+
timer = setInterval(() => {
90+
if (!isPaused && seconds > 0) {
91+
seconds--;
92+
countdown = `${seconds}`;
93+
document.title = "还有" + countdown + "秒爆炸!!";
94+
}
95+
if (seconds === 0 && timer) {
96+
clearInterval(timer);
97+
timer = null;
98+
document.title = boom;
99+
}
100+
}, 1000);
101+
};
102+
const pause = () => {
103+
isPaused = true;
104+
};
105+
const resume = () => {
106+
if (seconds > 0 && isPaused) {
107+
isPaused = false;
108+
}
109+
};
110+
111+
start();
112+
document.addEventListener("visibilitychange", () => {
113+
if (document.hidden) {
114+
if (timer) {
115+
resume();
116+
} else {
117+
document.title = boom;
118+
}
119+
} else {
120+
if (timer) {
121+
pause();
122+
}
123+
document.title = title;
124+
}
125+
});
79126
</script>
80127

81128
<style>

0 commit comments

Comments
 (0)