gpt4 book ai didi

javascript - Google 表格中的倒数计时器

转载 作者:行者123 更新时间:2023-11-30 19:09:06 24 4
gpt4 key购买 nike

我搜索了其他问题和 google/YouTube,从我的发现来看,这似乎是可能的,但我还没有找到这个确切的用例作为示例。作为序言,我对编码有一点点的工作知识,但不是很多。

这就是我想要完成的事情:我想在 Google 表格中创建一个具有 2 个可能功能(取决于用例)的倒数计时器:
1) 我可以在特定单元格或代码本身中输入计时器倒计时的结束日期。
2) 使用动态日期(即从计时器创建之日起 30 天)

所以我被卡住了......我尝试使用 script.google.com 并将这个脚本粘贴到我在网上找到的倒数计时器:

<!-- Display the countdown timer in an element -->
<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

// Get today's date and time
var now = new Date().getTime();

// Find the distance between now and the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>

但是当我运行代码时,出现“语法错误。(第 7 行,文件“倒计时”)”。

我需要对代码进行哪些修改才能使其正常工作?还是我使用了过于复杂的代码来创建计时器,而有更简单的方法?

最佳答案

您是否将其保存为 HTML?还是作为 JS?因为您应该将其保存为 .html,因为您的 Javascript 位于标签内,而不是单独的文件。您从 w3schools 提取的代码只是一个片段,我已经添加和标记并且它工作得很好。在您的示例片段中,您还将其粘贴到 Javascript 框中而不是 HTML 框中。试试这个例子,它正是您所拥有的代码!

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
text-align: center;
font-size: 60px;
margin-top: 0px;
}
</style>
</head>
<body>

<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

// Get today's date and time
var now = new Date().getTime();

// Find the distance between now and the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>

</body>

另一种方法是将 javascript 和 html 实际分离到单独的文件中,以使其更清晰,并使脚本和 HTML 分开。例如命名你的 HTML index.html 和你的脚本 timer.js然后在你的 HTML 中,而不是大块的脚本,你只需做 <script src="/timer.js"></script>

// Save this as for example timer.js

// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

// Get today's date and time
var now = new Date().getTime();

// Find the distance between now and the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
<!-- Save this as index.html -->

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
text-align: center;
font-size: 60px;
margin-top: 0px;
}
</style>
</head>
<body>

<p id="demo"></p>

<!-- Load the Javascript from a separate file called timer.js -->
<script src="/timer.js"></script>

</body>

关于javascript - Google 表格中的倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58682268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com