javascript settimeout在递归函数中不起作用

时间:2014-09-09 10:46:55

标签: javascript jquery recursion settimeout

我的脚本如下所示,它应该在div中重放鼠标图像,但是settimeout不起作用,并且控制台中也没有错误:

function play(data, value) {
    var data = data;

    function run() {
        var nowTime;
        var newdata = data.splice(0, 1); // after splice, data will be auto updated
        if (newdata.length == 1) {
            nowTime = newdata[0][6];
            var timer = setTimeout(function() {
                if (newdata[0][3] == '14') {
                    replay(newdata[0][0], newdata[0][1]);
                }
                preTime = nowTime;
                // continue run next replay
                run();

            }, nowTime - preTime);
        }
    }
    run();
}

请帮帮我。如何解决这个问题。

提前致谢

1 个答案:

答案 0 :(得分:1)

试试这个

var newdata;
var nowTime;
var preTime;
function play(data, value)
{
  newdata= data.splice( 0, 1 ); // after splice, data will be auto updated

  if ( newdata.length ==  1 ) {
        nowTime = newdata[0][6];
        var timer = setTimeout("timer();",nowTime - preTime );   
  }
}
function timer()
{
  if(newdata[0][3] == '14'){
     replay( newdata[0][0], newdata[0][1]);
  }
  preTime = nowTime;
  play();
}
play();