简体   繁体   中英

JavaScript ignore alert if reloading page

I am detecting the end of a webrtc stream in JavaScript like this...

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.

Like

this.isVideoRunning = false;

On 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

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

(I wanted this to be comment but I am not allowed to comment yet)

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