让我解释一下。我希望我的程序能做到以下几点:
1º要求用户输入以秒为单位的时间值2º从用户完成的值开始倒计时并向他显示3º当它结束时,它会提醒用户时间已经结束。
这是我到目前为止的代码(我不知道如何做,我已经尝试了很多次(,我用提示和警报做了它,但可能使用html有一个解决方案
let counter= prompt("Please, select in how much time (in seconds) do you want me to alert you")
{
SetTimeOut(() =>
{
(console.log("Alert: you have completed your time"), counter)
})
}
while (setTimeout>0)
{
alert("your time finishes in: "+ counter + "seconds")
}
首先是setTimeout
而不是SetTimeOut
JavaScript在一个线程中运行,在回调/承诺上工作,而不是在循环时工作
// get the value
// convert it to number using `+` operator
// multiply by 1000 to get milliseconds
const counter = +prompt("Please, select in how much time (in seconds) do you want me to alert you") * 1000
// set timeout to run after `counter` milliseconds
setTimeout(() => {
// log alert
console.log("Alert: you have completed your time", counter)
}, counter)