首页 > 使用callback报错是怎么回事?

使用callback报错是怎么回事?

/* show page 1 words */
    showWord(w1,document.getElementById('w1'),0,function(){
        alert(111);
    });

    /* show function */
    function showWord(word,objID,i,callback){
        var wlen = word.length;
        objID.innerHTML += word[i];
        i++;
        if(i < wlen){
            setTimeout(function(){
                showWord(word,objID,i);
            },500);
        }else if(i == wlen){
            callback();
        }

    }

为什么在callback();这里时,会报错呢?
错误是:
Uncaught TypeError: undefined is not a function index.html:73
showWord index.html:70
(anonymous function)

对callback的用法错了么?
应该怎样才是对的?


    if(i < wlen){
        setTimeout(function(){
            showWord(word,objID,i);   // 70行 , 这里没有第4个参数callback
        },500);
    }else if(i == wlen){
        callback();                   // 73行
    }

错误栈已经告诉你了, 73行 undefined is not a function, 上层调用是 70行


        setTimeout(function(){
            showWord(word,objID,i);
        },500);

这里少了一个callback

【热门文章】
【热门文章】