Not working, display javascript variable in html

David :

Simple question, the variable total is not being displayed in the html page, could you please tell me why this is not working? I am quite new to coding.

 var addtime = [0,2];

 function totaltime(){
  var total = 0;
   for(var i in addtime) {
     total += addtime[i];
   }
   document.getElementById("totalTime").innerHTML = total;
 }

html

<h1>Total Time: <span id="totalTime"></span></h1>
Hans Felix Ramos :

You are missing call the function totaltime();

var addtime = [0,2];

function totaltime(){
   var total = 0;
   for(var i in addtime) {
      total += addtime[i];
   }
   document.getElementById("totalTime").innerHTML = total;
 }
 
 totaltime();
   <h1>Total Time: <span id="totalTime"></span></h1>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=405814&siteId=1