不知道为什么这不起作用 - Javascript基本增量功能

Der*_*rek 0 javascript

我正在编写代码来增加最基本的进度条......它只是不起作用.这些是使用的变量:

map.progress_bar = // the progress div that grows inside a container div that is the width of the screen (100%);

progress = 0;

state.window_width = // the width of the window or otherwise $(window).width();

setTimeout(function incProgress(){
        if ( map.progress_bar.width() < state.window_width ) {
            progress += 10;
            map.progress_bar.css({
                width: String(progress + '%')
            });
            console.log('progress: ', map.progress_bar.width());
            console.log('window: ', state.window_width);
            setTimeout(incProgress(), 300);
        }
    }, 300);
Run Code Online (Sandbox Code Playgroud)

请不要让我做setInterval.请向我解释为什么在地球上这不起作用,我感到非常不开心.

Que*_*tin 5

setTimeout(incProgress(), 300);

您正在调用该函数并将其返回值(undefined)传递给setTimeout.

你需要一个传递函数setTimeout.

删除().