Justin Lachapelle Justin Lachapelle - 6 months ago 20
HTML Question

Javascript Loadingbar

I am attempting to create a loading bar in javascript

here is my code:

function load(barsize){
parseInt(barsize);

var loadingbar = '';
for(i = 1; i<=barsize;i++){
loadingbar += '<div style="width:5px; height: 5px; float:left;background-color:green;"></div>'
}
document.getElementById('loadingbar').innerHTML = loadingbar;
if(barsize < 80){
barsize++;
timeout = window.setTimeout('load('+barsize+')', 100);
}
}


The debugger is saying "Uncaught ReferenceError: load is not defined" after doing nothing with the statement:

timeout = window.setTimeout('load('+barsize+')', 100);

Answer

Change your :

timeout = window.setTimeout('load('+barsize+')', 100);

with :

timeout = window.setTimeout(function() { load(barsize); }, 100);

This way you can pass params to the load function without getting the undefined error