0

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

4
  • 3
    Scope of run() is only inside play() method. You can not call run method outside of play() Commented Sep 9, 2014 at 10:48
  • yah that's right I pasted it wrong ..but still its not working Commented Sep 9, 2014 at 10:50
  • Its better to use setInterval() than using setTimeout() repeatedly Commented Sep 9, 2014 at 10:51
  • @AnoopJoshi I am new to javascript please can you provide me some example to do the same mouse image replay using setinterval Commented Sep 9, 2014 at 10:54

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

splice is not existing by default in javascript @anoop joshi added this by some prototypes to all objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.