what does this javascript logic mean?


what does this javascript logic mean?



I can't wrap my head around this code from w3schools for a javascript play/pause event.




let video = document.getElementById("myVideo");
let btn = document.getElementById("myBtn");

function myFunc() {
if (video.paused) {
video.play();
btn.innerHTML = "Pause";
} else {
video.pause();
btn.innerHTML = "Play";
}
}



if the video paused is true then the video should play and the btn should display Pause. But shouldn't it be the other way round? If the video is paused then video.pause() should be there?


video.pause()





Think of what the intent of pushing the button is, and what the text on the button indicates. The text tells you what happens if you hit the button, not what's currently happening.
– Carcigenicate
Jun 30 at 18:38






Take a look at your favorite YouTube video. What symbol is displayed while it's playing? When it's paused?
– Elan Hamburger
Jun 30 at 18:40





So according to you, it should be if (video.paused) { video.pause() } ?
– TiiJ7
Jun 30 at 18:41


if (video.paused) { video.pause() }





btn.innerHTML is to set information about the next action the user could do
– Emeeus
Jun 30 at 18:42




1 Answer
1



Remember, the action is performed when you click the button, so imagine if the video is playing and the function is executed, then the following snippet runs:


video.pause(); // the played video is now paused
btn.innerHTML = "Play"; // the button now shows 'Play' from what it was before 'Pause'



When the function is executed again (when it is paused) the code checks if it is paused and if it is then it means it needs to play hence the following snippet:


if (video.paused) { // Was the video paused? If yes...
video.play(); // We now play the video as it was paused
btn.innerHTML = "Pause"; // We changed the button text from 'Play' to 'Pause'
}



The comments in the above snippets should clarify further.





thank you, that makes it a lot more clear. I am thinking about this the wrong way.
– ZeWorks
Jun 30 at 21:53






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API