javascript - 1700 小时后的暗模式
问题描述
我正在尝试在我的网页上一段时间(1700 小时)后将背景和一 (1) 个标题更改为不同的颜色。但是,它不会触发。我在这里做错了什么?
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dayLight = 0900;
var lightMode = 1700;
if (time >= dayLight && time <= lightMode) {
document.body.style.backgroundColor = "white";
document.querySelector("h1.example").style.color = "black";
console.log(time + " " + "=" + " " + "Light Mode!");
} else {
document.body.style.backgroundColor = "black";
document.querySelector("h1.example").style.color = "white";
console.log(time + " " + "=" + " " + "Dark Mode!");
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dark Mode Example</title>
</head>
<body>
<h1 class="example">Dark Mode Example</h1>
<p>Test Paragraph</p>
</body>
<script src="script.js"></script>
</html>
解决方案
//I just set an interval for every second to check the time again
var s=setInterval(()=>{
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dayLight = 0900;
var lightMode = 1700;
if (time >= dayLight && time <= lightMode) {
document.body.style.backgroundColor = "white";
document.querySelector("h1.example").style.color = "black";
//console.log(time + " " + "=" + " " + "Light Mode!");
} else {
document.body.style.backgroundColor = "black";
document.querySelector("h1.example").style.color = "white";
//console.log(time + " " + "=" + " " + "Dark Mode!");
}
},1000)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dark Mode Example</title>
</head>
<body>
<h1 class="example">Dark Mode Example</h1>
<p>Test Paragraph</p>
</body>
<script src="script.js"></script>
</html>
推荐阅读
- html - 我想设置一个在所有网站上垂直居中的照片库
- reactjs - 使用 styled-components 任意注入样式
- ruby-on-rails - 如何让 PostCSS Autoprefixer 与 Rails 6 一起使用
- python - 从 Python 字典中获取项目数据时出错
- matlab - 存储信号值,作为函数输入的向量输出
- jsf - PrimeFaces 7.0 Ajax 更新警告
- c# - 从多个线程调用 CPython
- python-3.x - Azure 逻辑应用程序中 Python 脚本的 Docker 问题未连接到当前 Azure Blob 存储
- javascript - 下载文件时元素无法自动识别文件扩展名
- c# - 引用子类作为父类变量