[英]Function to listen for global variable change?
Novice javascript question here: I have two functions. 新手javascript问题:我有两个功能。 The first function returns data from a video player api when the video player is playing.
第一个函数在视频播放器播放时从视频播放器api返回数据。 The second function should execute code depending on whether function 1 is retrieving data.
第二个函数应该根据函数1是否正在检索数据来执行代码。
Something like this: 像这样的东西:
var playing = false;
// Function 1
function onPlayProgress(data, id) {
if (data) {
playing = true;
}
}
// Function 2
function movePlayhead() {
if(playing) {
console.log('playhead moving')
} else {
console.log('playhead stopped')
}
}
movePlayhead();
My problem is function 2
is only called when the file loads, not continuously. 我的问题是
function 2
仅在文件加载时调用,而不是连续调用。 How can I do this? 我怎样才能做到这一点?
Use setInterval . 使用setInterval 。
setInterval(function() {
if (playing) {
...
} else {
...
}
}, 500);
Or (as @bfavaretto pointed out): 或者(正如@bfavaretto指出的那样):
setInterval(movePlayhead, 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.