gpt4 book ai didi

javascript - 你如何每秒加1?

转载 作者:技术小花猫 更新时间:2023-10-29 11:48:50 25 4
gpt4 key购买 nike

我都试过了,我做不到!!!这是我的代码,请帮忙:

<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="favicon.ico"/>
<title>Doughtnut Box !</title>
<!--Design-->
<style>

</style>
<!--jQuery and Javascript-->
<script>
var doughnut = 0;
window.setInterval(
doughnut + 1
,1000);
document.getElementById("doughnuts").innerHTML="You have " + doughnut >+ " doughnuts!";
</script>
</head>
<body>
<h3 id="doughnuts"></h3>
</body></html>

感谢您的代码,它真的很棒!现在我该如何保存它,以便在我刷新时它不会重新开始?

最佳答案

将它们放在一个函数中。此外,将您的表达更改为作业

<script>
var doughnut = 0;
window.setInterval(
function () {
doughnut = doughnut + 1;
document.getElementById("doughnuts").innerHTML = "You have " + doughnut + " doughnuts!";

}, 1000);
</script>

下面的可运行演示:

var doughnut = 0;
window.setInterval(function () {
doughnut = doughnut + 1;
document.getElementById("doughnuts").innerHTML = "You have " + doughnut + " doughnuts!";
}, 1000);
<div id="doughnuts"></div>

关于javascript - 你如何每秒加1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19683761/

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