By employing timeUpdate in conjunction with currentTime

I am looking to utilize the timeUpdate and currentTime functionalities to display or hide a div at a specific time during video playback.

I have been able to make it work, but it seems to be showing and hiding every second I specify.

Below is the code I have attached... is there a way to make it display without the constant showing and hiding?

var video_one = document.getElementById('video_one'),
video_two = document.getElementById('video_two'),
video_one_wrapper = document.getElementById('video_one_wrapper'),
video_two_wrapper = document.getElementById('video_two_wrapper'),
play_button = document.getElementById('play_button');

// add event listener to play button
play_button.addEventListener('click', play_videos);

// run function on click
function play_videos() {
video_one.play();
video_two.play();
}

var switch_button = document.getElementById('switch_button');

switch_button.addEventListener('click', switch_videos);

function switch_videos() {
console.log('click');

if(video_one_wrapper.classList.contains('video_active')) {
console.log('it contains');
video_one_wrapper.classList.remove('video_active');
video_two_wrapper.classList.add('video_active');
} else if(video_two_wrapper.classList.contains('video_active')) {
video_two_wrapper.classList.remove('video_active');
video_one_wrapper.classList.add('video_active');
console.log('it doesnt')
}
}

video_one.addEventListener("timeupdate", message_one);
video_two.addEventListener("timeupdate", message_two);

var message_one = document.getElementById("message_one"),
message_two = document.getElementById("message_two");


function message_one() {
// if time between 10s and 20s
if(this.currentTime > 1 && this.currentTime < 20) {

if(message_one.classList.contains("message_hide")) {
message_one.classList.remove("message_hide");
} else {
message_one.classList.add("message_hide")
}
}
}

function message_two() {
// if time between 10s and 20s
if(this.currentTime > 1 && this.currentTime < 20) {

if(message_two.classList.contains("message_hide")) {
message_two.classList.remove("message_hide");
} else {
message_two.classList.add("message_hide")
}
}
}
body {
margin: 0;
font-family: "Helvetica Neue";
font-size: 16px;
color: #FFF;
background-color: #242424;
}

.landing {
width: 100vw;
height: 100vh;
}

.landing_wrapper {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}

.title_wrapper {
text-align: center;
}

.title_wrapper > h1 {
font-size: 46px;
text-transform: uppercase;
letter-spacing: 2px;
color: #FFF;
}

.title_wrapper > button {
background-color: #FFF;
border: none;
outline: none;
font-size: 16px;
text-transform: uppercase;
padding: 10px 20px;
}

video {
width: 100%;
z-index: 100;
}

.video {
position: relative;
width: 100vw;
height: 100vh;
}

.video_wrapper {
position: relative;
top:0;
left: 0;
width: 100%;
height: 100%;
}

.video_item {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 99;
}

.switch_wrapper {
z-index: 100;
position: absolute;
top:0;
left: 0;
}

.video_element {
z-index: 100;
}

.message_wrapper {
position: absolute;
top: 0;
left: 0;
z-index: 200;
}

.message_hide {
display: none;
}

.video_one {
z-index: 0;
}

.video_two {
z-index: 0;
}

.video_active {
z-index: 10;
}

.video_three {
}
<!doctype html>

<html lang="en">
<head>
<meta charset="utf-8">
<title>Title Here</title>
<meta name="description" content="The HTML5 Herald">
<meta name="author" content="SitePoint">
<link rel="stylesheet" href="main.css">

</head>

<body>

<div class="landing">
<div class="landing_wrapper">

<div class="title">
<div class="title_wrapper">
<h1>Title Here</h1>
<button id="play_button">Start</button>
</div>
</div>

</div>
</div>

<div class="video">

<div class="switch_wrapper"><button id="switch_button">Switch</button></div>

<div class="video_wrapper">



<div id="video_one_wrapper" class="video_item video_active">

<div id="message_one" class="message_wrapper message_hide"><h1>Hello Video 1</h1></div>

<video id="video_one" class="video_element" src="http://vid1308.photobucket.com/albums/s607/Billy_Adam_Skinner/girl_1_zps8ud1zuxh.mp4" loop >

</div>
<div id="video_two_wrapper" class="video_item">

<div id="message_two" class="message_wrapper message_hide"><h1>Hello Video 2</h1></div>

<video id="video_two" class="video_element" src="http://vid1308.photobucket.com/albums/s607/Billy_Adam_Skinner/boy_zpsugnp76od.mp4" muted loop>
</div>

</div>



</div>

  <script src="js.js"></script>

</body>
</html>

Answer №1

When an audio or video is playing, the timeupdate event is triggered to signify a change in the playing position. For instance, the function message_one is called every second while the video is playing. Here's a step-by-step example:

  1. currentTime = 1 second
  2. message_one function is executed
  3. The message_one element does not have the "message_hide" class
  4. The message_one element now has the "message_hide" class added
  5. currentTime is now at 2 seconds
  6. message_one function is called again
  7. The message_one element currently has the "message_hide" class
  8. message_one.classList.add("message_hide")
    is invoked to hide the message

Every time the currentTime updates, it alternates between showing and hiding the message.

Your current function is as follows:

function message_one() {
  // if time is between 1s and 20s
  if (this.currentTime > 1 && this.currentTime < 20) {
    if (message_one.classList.contains("message_hide")) {
      message_one.classList.remove("message_hide");
    } else {
      message_one.classList.add("message_hide")
    }
  }
}

One possible solution is to modify it as:

if (this.currentTime > 0 && this.currentTime < 20) {
    message_two.classList.remove("message_hide");
 }
 else {
    message_two.classList.add("message_hide")
 }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring Non Blocking IO through an Example

While browsing through a node.js tutorial, I stumbled upon this informative page that uses the example of a "Restaurant service" to explain a scenario. In the context of Blocking IO, they provide the following code snippet: // requesting drinks for table ...

Apply CSS styling to the v-html output

I am trying to enhance the style of HTML code using a v-html but have been struggling with finding a working solution so far. Can anyone help me out? :( Below is my code snippet: Template : <div class="para" v-html="value" /> Script : exp ...

Including item from ajax not within $.when function finished

function fetchData(){ return $.ajax({ url : 'URL1', data : { id : id }, type : 'GET', }); } function fetchItemData(item_id) { return $.ajax({ url: 'URL2', data: { item_id: it ...

Enhance data granularity in Jquery Flot by zooming in as the user interacts

I'm currently working on creating an interactive chart that allows users to zoom in for a more detailed view of the data. For instance, when viewing data over a default zoom period of 3 months, there would be one data point displayed every 24 hours. H ...

Best practices for retrieving information from the user interface

My journey with NextJS has been a learning experience as I navigate connecting the frontend to the backend. Initially, I attempted to create a Restful API for CRUD operations from the frontend, but encountered authentication issues that left me puzzled. A ...

TypeScript Generic Functions and Type Literals

Everything seems to be running smoothly: type fun = (uid: string) => string const abc: fun = value => value const efg = (callback:fun, value:string) =>callback(value) console.log(efg(abc, "123")) However, when we try to make it generic, we e ...

The z-index of the fixed header in FullPageJS is causing the section's content to be hidden

For my current project, I am incorporating FullPageJS and using a fixed top menu that adjusts its height based on the viewport size. On smaller screens, the menu items will be stacked on top of each other, while on wider screens, the menu items will be po ...

Is it necessary to use the strict doctype if our code is already valid with the transitional doctype?

Are there any drawbacks to using a transitional doctype with presentational or deprecated elements, even if our code is valid in W3C validation with a transitional doctype? Will we need to rewrite or edit the code of websites using XHTML/HTML transitional ...

Minimize or conceal iframe

This iframe contains a Google form that cannot be edited. I am looking for a way to close or hide this iframe, either through a button, a popup window button, or without any button at all. The $gLink variable holds the Google form link through a PHP sessio ...

Transferring data in PDF format through email with the help of PHPMailer and html2pdf

Having trouble sending PDF or PNG files over email despite multiple attempts. Despite reading countless articles on the topic, nothing seems to be working as suggested. Can anyone provide assistance? I am using PHPMailer along with html2pdf and html2canva ...

Unable to generate this QUIZ (JavaScript, HTML)

Hi there, I'm working on a sample quiz and having trouble displaying the result. As a coding newbie, I could really use some help. In this quiz, users answer questions and based on their responses, I want to display whether they are conservative, agg ...

Basic stacked or segmented bar in HTML

Instead of starting from scratch, I'm in search of insights on how to develop a stacked/segmented bar or find an existing control for this purpose. Here are my requirements: Horizontal bar Standard html Color-coded segments using CSS Segments propor ...

Tips for concealing a div in JavaScript when other divs are not present

Is there a way to hide the title div if related divs are not present in the HTML structure? This is the main HTML structure: <div class="row parent"> <div id="title-1" class='col-12 prov-title'> <h2 ...

Retrieve user choices from dropdown menu with Flask and HTML

I'm currently working on developing a dropdown list in HTML that, upon selection submission, will pass the chosen option to a Python script to dynamically update the content of the HTML page. While I have successfully implemented the dropdown list, I ...

My vanilla JavaScript code isn't running the smooth scroll function as expected

Struggling to incorporate smooth scroll on Safari using Vanilla js. Despite following the documentation to a tee, the implementation is still not functioning as expected. Outlined below is the vanilla.js code: (function() { scrollTo(); })(); function ...

Vue.js - dynamically applying CSS classes based on conditions

Within a VueNative project that utilizes NativeBase as a component library, with tags prefixed by nb-, the code snippet below is found within the <template> tags of the footer.vue file which houses navigation buttons. This piece of logic successfully ...

Looking to retrieve an element from an array of objects based on consecutive object properties, I have a specific value to search for

const levels = [ { indexId: 'A', level: 1, name: 'A', parent: '0', }, { indexId: 'A-A1', level: 2, name: 'A1', parent: 'A', }, { ...

"Text data will be automatically cut at the end of a page and continue

I encountered the following issue As depicted in the image The data is not being displayed properly In the next td, the last character 3 is also printed on the next line Is it possible to avoid page footer and header without adjusting the page margin? ...

Learn the art of blurring elements upon clicking in Vue

I've been attempting to trigger the blur event on an element when it is clicked, but I haven't been able to locate any helpful examples online. My initial approach looked like this: <a @click="this.blur">Click Me</a> Unfortunately, ...

Is it possible to use an SVG image as a border with a variable height in CSS

Check out this jsfiddle for my current project's nav setup. I want to center the logo in the middle of the nav with three links centered around it. However, I need the design to be responsive across various screen widths. Any advice on how to achieve ...