Incorporating various criteria within a function

Struggling to add 2 conditions in 1 function, need some guidance. Here's the code I have:

 var timeCheck = function(){
    var timePos = videoBGPlayer.currentTime; 
    if (timePos > 1){
        TweenLite.to(videoBGPlayer, 3, { marginTop: -130});
        videoBGPlayer.removeEventListener('timeupdate', timeCheck, false);
    } 
};

My goal is to make the video move up when its timePos is 1 and go back to its original position when timePos is 7.

Any assistance on this matter would be highly appreciated.

Answer №1

Start by checking if the variable timePos is greater than 7, and only proceed if it is also greater than 1

 let checkTime = function(){
    let timePos = videoPlayer.currentTime;

    if (timePos >= 7) {
        // resetting to original position
    } else if (timePos > 1){
        TweenLite.to(videoPlayer, 3, { top: -130});
        videoPlayer.removeEventListener('timeupdate', checkTime, false);
    } 
};

Answer №2

If you want to apply multiple conditions, you can do so by using JavaScript. Here's an example:

const checkTime = () => {
  let currentTime = videoPlayer.currentTime;
  
  if (currentTime < 5){
    TweenLite.to( videoPlayer , 2 , { opacity: 1 } );
    videoPlayer.removeEventListener('timeupdate', checkTime, false);
  } 
  else if (currentTime === 5) {
    // Perform some action when time is 5
  }
};

Answer №3

This code snippet demonstrates a basic if/else if logic flow.

if (timePos == 1){
    TweenLite.to(videoBGPlayer, 3, { marginTop: -130});
    videoBGPlayer.removeEventListener('timeupdate', timeCheck, false);
} 
else if( timePos == 7){
    //Move video down
}

It is important to note that specifying timePos > 1 instead of timePos == 1 can lead to unexpected behavior. For example, if the condition is never met for timePos = 1, the else if statement for timePos == 7 will not be executed.

Answer №4

Is this what you had in mind?

function checkTime() {
    var timePosition = videoPlayer.currentTime; 
    if (timePosition >= 1 && timePosition < 7) {
        TweenLite.to(videoPlayer, 3, {top: -130});
        videoPlayer.removeEventListener('timeupdate', checkTime, false);
    } else if (timePosition >= 7) {
        TweenLite.to(videoPlayer, 3, {top: 0});
    } 
}

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

Unable to make a POST request to the GitHub v3 API

I'm attempting to generate a public gist using JavaScript without any authentication - all purely client-side. var gist = { "description": "test", "public": true, "files": { "test.txt": { "content": "contents" ...

Tips for sequentially arranging and rearranging an array of numbers, even when duplicates are present

Encountered a perplexing issue that has me scratching my head in an attempt to visualize a solution. Currently, I am working with an array of objects that appears as follows: let approvers = [{order:1, dueDate: someDate},{order:2, dueDate: someDate}, ...

The Jquery .change() function refreshes the results just once

I am working on a form with 3 input fields named #first, #second, and #third, along with a fourth field labeled as #theResult. <div id="addFields"> <span id="addFieldsHeader">Add The Fields</span> <table style="margin:0 auto;"> ...

Using a locally hosted HTML page to update a JSON file stored on my computer

In my current project, I need to reorganize data stored in an array named unknown. Each item in this array needs to be moved either to a new array called good or another one called bad. let data = { "bad":[], "unknown": ["b", "a", "d", "g", "o", ...

Oops! The system encountered a problem: the property 'modalStack' is not recognized on the type 'NgxSmartModalService'. Maybe you meant to use '_modalStack' instead?

Currently, I'm facing an issue while attempting to run ng build --prod in my Angular 6 project. I have also incorporated the NgxSmartModal package for handling modals. Unfortunately, the build process is failing and I can't seem to figure out why ...

Issue with Empty Date Time Format in Vue2 Date-Picker

In our company, we use vue2-datepicker to manage the starting and ending times of meetings. The dates are stored in "YYYY-MM-DD HH:mm" format on the backend, but we convert them to "DD-MM-YYYY HH:mm" format when retrieving the data in the mounted() hook. T ...

Guide on transferring data from an API using mapped React hooks

I am working with react hooks and attempting to pass a value to the delete function and execute the put function <Fab aria-label="delete" className={classes.fab} value={vacation.id} ...

Use jQuery to detect the presence of the class .class, and if it exists, automatically append the same class to a specific #id element on a particular webpage

Need help with jQuery - adding a specific class to an element based on the presence of another class Hello everyone, I have searched through various forums and tried multiple code snippets in JavaScript and jQuery to no avail. Despite other scripts worki ...

Is there a way to transfer table row data to another table by simply clicking on the corresponding checkbox in the same row?

I'm working with a table that includes 4 fields: service, amount, tax, and action. My challenge is to have the data from any row in the first table added to a second table when its checkbox is selected. The second table should have the same fields a ...

Using Selenium WebDriver to handle Angular requests in Java

I am currently developing tests for an angular-based application and I find myself in need of assistance. The specific task at hand involves creating a mechanism that will wait until all pending requests within the application have been processed before pr ...

Having trouble grasping the error message "Uncaught Typerror Cannot Read Property of 0 Undefinded"?

As I embark on creating my very first ReactJS website with Node in the back-end, I encountered an issue in fetching and printing data. While I successfully displayed the names, pictures, and emails of project members from the server, I faced an error when ...

Navigate to a specific div with its id in ReactJS without relying on external libraries

Is it possible to scroll to a specific div using an ID in React without relying on external libraries? I've come across some solutions that involve scroll libraries. Below is the code for the div within my WrappedQuestionFormFinal component: <div ...

Employing a lexicon of hexadecimal color code values to harmonize CSS styles

I have a unique requirement where I need to utilize a dictionary of HTML color codes and then apply those colors as styles. It's an interesting challenge! Here is an example of how my color dictionary looks like: const colorCodes = { red: ...

When a cookie is set in NextJS with a static export, it reverts back to its original

My current project involves building a multilingual website. To handle language selection, I have implemented a system where the chosen language is stored in a cookie and retrieved using getInitialProps in the _app file, which is then passed as a context A ...

Learn how to efficiently execute a function multiple times using pure JavaScript

I am trying to create a tabbed content functionality with multiple elements. How can I utilize the same function for various elements declared in a variable? For example, I want to clone the parent div.tabs element with similar content but different ids an ...

Utilizing an array to pass a series of items to a function parameter

I am currently working on an Angular project that involves a controller and service. In this setup, the controller sends data and an endpoint to the service. As of now, the service handles the http request. However, I am in the process of refactoring my ...

What is the fastest way to invoke the driver.quit() method in Selenium?

Is there a way to force close the browser immediately instead of waiting for it to complete loading before calling driver.quit()? ...

Using Web SQL always seems to throw me for a loop

I attempted to populate a web SQL database with some data, but encountered an issue. Here is my code: database(); for(var i=0; i<m.length; i++){ showid = m[i].id; showtitle = m[i].title; insert(); } function database(){ //open the database ...

Managing and comparing category IDs in JavaScript to effectively store and render subcategories

My goal is to set the current category ID in a variable, and if the category changes, I want to store that as well. Then, I need to compare both IDs. If they are not equal, I want to set the subcategory to null. However, I am unsure of where my mistake lie ...

Having trouble accessing parameter values in a React app using ES6 arrow functions

Hey there! I've been attempting to send values to an ES6 arrow function in my React application, but unfortunately, it's not working. Here's what I'm trying: var data = []; for (var i=1; i<=5; i++) { data.push(<li><a href ...