简体   繁体   中英

javascript settimeout not working inside recursive function

My script is as follows which should replay mouse image inside a div but settimeout is not working and there is no error in console also:

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();
}

Please help me. How to solve this issue.

thanks in advance

try this

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM