javascript - 在 jQuery 中定义时间?
问题描述
我正在用 jQuery 制作一个计时器。我是新手。当我检查错误时,它说
- 'end_time' 未定义。
- 'start_time' 未定义。
- 'formatted_time' 未定义。
- 'time_change' 未定义。
有人可以帮助我指出我将如何定义它们的正确方向吗?
另外,我收到一个错误:
- 单击“重置”按钮:选择器“#reset”未呈现属性“显示”的预期 CSS:预期“内联块”深度等于“无”
这是我的html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Interactivity</title>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="styles/site.css"/>
<script src="scripts/jquery-3.2.1.min.js"></script>
<script src="scripts/formatTime.js"></script>
<script src="scripts/times.js"></script>
<script src="scripts/reset.js"></script>
</head>
<body>
<div id="text">
<p>
Can you internally count 45 seconds precisely?
</p>
</div>
<button id="start">Start Timer</button>
<button id="stop" style="display: none;">Stop Timer</button>
<button id="reset" style="display: none;">Reset Timer</button>
<span id="time_started" class="hidden" style="display: none;">Timer Started</span>
<span id="time_ended" class="hidden" style="display: none;">Timer Ended</span>
</body>
</html>
这是我的CSS:
body {
background-color: white;
font-family: sans-serif;
margin: 200px auto 0;
max-width: 900px;
}
h1 {
text-align: center;
}
div {
margin-bottom: 50px;
}
.hidden {
display: none;
}
这是times.js:
/* global formatTime: true */
/* Please do not remove the comment above. */
// timer to calculate the starting and stopping clicks
$(document).ready(function() {
$("#start").on('click',function() {
$("#start").hide();
$("#stop").show();
$("#time_started").hide();
$("#time_ended").hide();
end_time = new Date();
start_time = new Date();
formatted_time = formatTime(start_time);
});
$("#stop").on('click',function() {
$("#stop").hide();
$("#reset").show();
$("#time_started").hide();
$("#time_ended").show();
end_time = new Date();
formatted_end_time = formatTime(end_time);
$("body").append("<p class='results'>You started at "+formatted_time+".</p>");
$("body").append("<p class='results'>You finished at "+formatted_end_time+".</p>");
time_change = end_time-start_time;
$("body").append("<p class='results'>You counted "+(time_change/1000)+" seconds.</p>");
$("body").append("<p class='results'>You are off by "+(time_change/1000-45)+" seconds.</p>");
});
});
这是 formatTime.js:
// formats the current date/time so that it reads as hh:mm:ss PM/AM
function formatTime(time) {
var
end_time,
formatted_time,
formatted_end_time,
start_time,
hour = 12,
minute = 10,
second = 10,
meridies;
hour = time.getHours();
if (hour>12) {
hour = hour-12;
meridies = "PM";
} else {
meridies = "AM";
}
minute = time.getMinutes();
if (minute<10) {
minute = "0"+minute;
}
second = time.getSeconds();
if (second<10) {
second = "0"+second;
}
return hour+":"+minute+":"+second+" "+meridies;
}
这是reset.js:
// reset everything
$("#reset").on('click',function() {
$(".results").addClass("hidden");
$("#reset").addClass("hidden");
$("#start").removeClass("hidden");
$("#time_started").addClass("hidden");
$("#time_ended").addClass("hidden");
});
解决方案
$(function() {
// Define more Global Variables
var end_time, start_time, formatted_time, formatted_end_time;
// Define Functions
function formatTime(dt) {
return dt.getSeconds();
}
// Define Event Callbacks
$("#start").on('click', function() {
$("#start").hide();
$("#stop").show();
$("#time_started").hide();
$("#time_ended").hide();
end_time = new Date();
start_time = new Date();
formatted_time = formatTime(start_time, end_time);
});
$("#stop").on('click', function() {
$("#stop").hide();
$("#reset").show();
$("#time_started").hide();
$("#time_ended").show();
end_time = new Date();
formatted_end_time = formatTime(end_time);
$("body").append("<p class='results'>You started at " + formatted_time + ".</p>");
$("body").append("<p class='results'>You finished at " + formatted_end_time + ".</p>");
var time_change = end_time - start_time;
$("body").append("<p class='results'>You counted " + (time_change / 1000) + " seconds.</p>");
$("body").append("<p class='results'>You are off by " + (time_change / 1000 - 45) + " seconds.</p>");
});
});
body {
background-color: white;
font-family: sans-serif;
margin: 200px auto 0;
max-width: 900px;
}
h1 {
text-align: center;
}
div {
margin-bottom: 50px;
}
.hidden {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="text">
<p>
Can you internally count 45 seconds precisely?
</p>
</div>
<button id="start">Start Timer</button>
<button id="stop" style="display: none;">Stop Timer</button>
<button id="reset" style="display: none;">Reset Timer</button>
<span id="time_started" class="hidden" style="display: none;">Timer Started</span>
<span id="time_ended" class="hidden" style="display: none;">Timer Ended</span>
推荐阅读
- php - 如何在将数据发送到电子邮件时从单选按钮中选择值
- javascript - Uncaught ReferenceError: myFunction is not defined Javascript
- python - 如何逐元素比较 3 个 numpy 数组并将结果作为具有最大值的数组获得?
- selenium - 如何覆盖/替换微软基本编码器文件?
- css - 如何使引导侧边栏折叠不改变内容宽度?
- c# - 如何将 xamarin android 应用程序连接到 Cloud Firestore db
- python - 在 django 中按模型字段的子字符串对对象进行排序
- javascript - 如何从另一个 js 应用程序启动 js 应用程序?
- php - 用 JS 获取内容,用 PHP 回显
- azure-active-directory - 微软图形。使用应用程序自己的身份访问 OneDrive