【问题标题】:How to make JS counterUP save result in var and div如何让 JS counterUP 将结果保存在 var 和 div 中
【发布时间】:2016-11-29 20:53:30
【问题描述】:

我的代码遇到了困难。一切都运行良好,除了 var 计数器和 div 没有改变它们的值,因为你可以看到它总是在 26 上。我需要的是每次刷新时在我的网页上我需要查看更新的值网站等,但没有运气。

这里你可以看到代码:

 <head>       
    <script type="text/javascript">
    var counter = 26.00  
    var timer;

    function countUP () {
        counter = counter + Math.random();
        document.getElementById("hugee").innerHTML =(counter).toFixed(5);
    }

    </script>
</head>

<body onLoad='timer = setInterval("countUP()", 5000);'>
   <div class="col-xs-9 text-right">
     <div class="huge" id="hugee">26.00</div>
   </div>
</body>

【问题讨论】:

  • 您的代码当前会在 5 秒后更新 hugee 元素内的值。如果您需要在页面刷新时保留该值,可以使用 window.localStorage 对象。这篇文章对此进行了解释:stackoverflow.com/questions/16206322/…

标签: javascript html timer counter


【解决方案1】:
only wait 5 second
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>       
    <script type="text/javascript">
    var counter = 0;
    var timer;

    function countUP() {
        counter = counter + Math.random();
        document.getElementById("hugee").innerHTML =(counter).toFixed(5);
    }

    </script>
</head>

<body onLoad='timer = setInterval("countUP()", 5000);'>
   <div class="col-xs-9 text-right">
     <div class="huge" id="hugee"></div>
   </div>
</body>
    </html>

【讨论】:

  • 仍然没有保存 0 的值,所以下次我刷新页面时将使用更新的值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2020-02-15
  • 2021-10-08
  • 2021-10-31
相关资源
最近更新 更多