0

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);
  • 1
    Use `setTimeout(function() { load(barsize); }, 100);` – Tushar Jun 03 '16 at 15:58
  • @JustinLachapelle There are some working examples here: http://stackoverflow.com/questions/6258690/is-there-any-way-to-change-the-color-of-text-halfway-through-a-character-on-a – Midas Jun 03 '16 at 16:04

2 Answers2

0

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

Akram Saouri
  • 1,179
  • 8
  • 15
0

The problem lies in this part of the code..

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

Change this to.....

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

Hope this helps..

Glad to help you.

Sangamesh