为什么这个setInterval在for循环中不起作用?

时间:2015-11-24 09:31:36

标签: javascript

function countDown(){
    var count = ""
for(var i = 10; i > 0; i--){

    count = i;

    setInterval(function(){show(count)}, 1000);

      // i've try : setInterval(show(count), 1000);
    }   
}


function show(count){
        document.getElementById('tes').innerHTML = count;
    }
不要标记为重复,这是不同的事情! 这只是实验,

1 个答案:

答案 0 :(得分:0)

代码应为

function countDown(){
    var count = "";
for(var i = 10; i > 0; i--){

    count = i;

    setInterval(show(count), 1000); //call function, do not create one here

      // i've try : setInterval(show(count), 1000);
    }   
}


function show(count){
        document.getElementById('tes').innerHTML = count;
    }

countDown(); //invokation