简体   繁体   English

如果重新加载页面,JavaScript将忽略警报

[英]JavaScript ignore alert if reloading page

I am detecting the end of a webrtc stream in JavaScript like this... 我正在这样检测JavaScript中的webrtc流的结尾...

stream.getVideoTracks()[0].onended = () => {
    alert('Feed Has Ended');
};

This is working correctly, but if the user refreshes or reloads the page then the alert is also shown. 这可以正常工作,但是如果用户刷新或重新加载页面,则还会显示警报。

I understand that this is technically correct, but how can I get it to not display the alert under those conditions? 我了解这在技术上是正确的,但是如何在这种情况下不显示警报呢?

Why don't you use a global boolean to check if video is playing or not? 为什么不使用全局布尔值检查视频是否正在播放? When you will reload or refresh the page, isVideoRunning will become false and alert won't show. 当您重新加载或刷新页面时,isVideoRunning将变为false,并且不会显示警报。

Like 喜欢

this.isVideoRunning = false;

On addtrack, 在addtrack上,

this.rtcPeerCon_.ontrack = function (event) {
    if (!this.rtcPeerCon_) {
        return;
    }
    if( !this.remoteVideo_ ) {
        return;
    }
    this.remoteVideo_.srcObject = event.streams[0];
    this.isVideoRunning = true;
}

then in your onStream ended callback you can check 然后在onStream结束的回调中,您可以检查

if (this.isVideoRunning) {
    alert('whatever');
    this.isVideoRunning = false;
}

(I wanted this to be comment but I am not allowed to comment yet) (我希望对此发表评论,但尚未被允许发表评论)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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