2 回答
TA贡献1765条经验 获得超5个赞
您需要更新 render.js,并在 HTML 中添加字段来保存计时器值:
// In the render.js file.
function startTimer(){
// Retrieve the values of your inputs
var seconds = document.getElementById("seconds").value || 0;
var minutes = document.getElementById("minutes").value || 0;
var hours = document.getElementById("hours").value || 0;
// Calculate the total amount of milliseconds (1000 times the number of seconds).
var totalMilliseconds = 1000*(Number(seconds) + Number(minutes)*60 + Number(hours)*60*60)
// Start the timer with javascript builtin.
setTimeout(doSomething, totalMilliseconds)
}
function doSomething() {
// Do something
alert("Time is over!");
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body>
<label>Seconds: </label>
<input type="number" id="seconds" name="seconds" placeHolder="insert seconds"><br>
<label>Minutes: </label>
<input type="number" id="minutes" name="minutes" placeHolder="insert minutes"><br>
<label>Hours: </label>
<input type="number" id="hours" name="hours" placeHolder="insert hours"><br>
<button onclick="startTimer()">Start Timer</button>
<script src="./render.js"></script>
</body>
</html>
TA贡献1869条经验 获得超4个赞
最近,我创建了一个代码来显示给定分钟的倒计时,并通过提供的函数设置该值。你可以试试这个代码;
const countdown = (durationInMin, setter) => {
// added 1 second to start time, so this will start from 00 seconds
let startTime = Date.now() + 1000
let diff, min, sec
//updating timer by 1 second
let timer = setInterval(() => {
if (diff <= 0) {
//once the different reached zero, clearing the interval
clearInterval(timer)
return;
}
diff = durationInMin * 60 - (((Date.now() - startTime) / 1000) | 0)
min = (diff / 60) | 0
sec = (diff % 60) | 0
// formatting to two digits
min = min < 10 ? "0" + min : min
sec = sec < 10 ? "0" + sec : sec
// display format
let t = min + ":" + sec
// calling the setter function by passing the timer value
setter(t)
}, 1000)
}
您可以在此处指定durationInMin 的持续时间(以分钟为单位)和setter 函数以显示计时器的返回值。
您可以按如下方式调用使用它;
function setter(value) {
myGlobalVariable = value
}
startTimer() {
countdown(5,setter);
}
编辑:
当您提供代码时,您可以尝试以下操作;
渲染器.js
const countdown = (durationInMin, setter) => {
// added 1 second to start time, so this will start from 00 seconds
let startTime = Date.now() + 1000
let diff, min, sec
//updating timer by 1 second
let timer = setInterval(() => {
if (diff <= 0) {
//once the different reached zero, clearing the interval
clearInterval(timer)
return;
}
diff = durationInMin * 60 - (((Date.now() - startTime) / 1000) | 0)
min = (diff / 60) | 0
sec = (diff % 60) | 0
// formatting to two digits
min = min < 10 ? "0" + min : min
sec = sec < 10 ? "0" + sec : sec
// display format
let t = min + ":" + sec
// calling the setter function by passing the timer value
setter(t)
}, 1000)
};
function setter(value) {
var t = document.getElementsByClassName("timer")[0]
t.innerText = value.toString()
}
function startTimer() {
console.log("clicked")
countdown(0.5, setter)
}
索引.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body>
<button onclick="startTimer()">Start Timer</button>
<div class="timer">--:--</div>
<script src="./renderer.js"></script>
</body>
</html>
- 2 回答
- 0 关注
- 102 浏览
添加回答
举报