Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

javascript - 在执行下一行之前等待5秒(Wait 5 seconds before executing next line)

This function below doesn't work like I want it to;

(下面的这个功能不像我想要的那样工作;)

being a JS novice I can't figure out why.

(作为一个JS新手我无法弄清楚为什么。)

I need it to wait 5 seconds before checking whether the newState is -1 .

(我需要等待5秒才能检查newState是否为-1 。)

Currently, it doesn't wait, it just checks straight away.

(目前,它不会等待,它只是立即检查。)

function stateChange(newState) {
  setTimeout('', 5000);

  if(newState == -1) {
    alert('VIDEO HAS STOPPED');
  }
}
  ask by copyflake translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to put your code in the callback function you supply to setTimeout :

(您必须将代码放在您提供给setTimeout的回调函数中:)

function stateChange(newState) {
    setTimeout(function () {
        if (newState == -1) {
            alert('VIDEO HAS STOPPED');
        }
    }, 5000);
}

Any other code will execute immediately.

(任何其他代码将立即执行。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...