2016-11-29 50 views
0

我的代碼遇到困難。一切運行良好,除了var計數器和div不改變它們的值,因爲你可以看到它的始終在26.我每次刷新時需要的是我的網頁上我需要看到一個更新的值我嘗試搜索許多論壇網站等,但沒有運氣。如何讓JS counterUP在var和div中保存結果

在這裏你可以看到代碼:

<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> 
+0

你的代碼目前5秒後更新hugee元素中的值。如果您需要在整個頁面刷新時保持該值,則可以使用window.localStorage對象。這是在這篇文章中解釋:http://stackoverflow.com/questions/16206322/how-to-get-js-variable-to-retain-value-after-page-refresh – Mino

回答

0
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

仍然沒有保存他值0下一次我刷新頁面將與更新的價值 –