Apply SetTimeout exclusively for desktop devices

A website I'm working on has a background video from YouTube using YTPlayer. To enhance the user experience, I have implemented a CSS spinner that displays while the page is loading. However, I noticed that the spinner disappears before the video finishes loading and starts playing.

$(window).load(function() {
    setTimeout(function(){
    $('.loader').fadeOut(1000);    
    $('.loader-bg').fadeOut(1000);
    },4000)
    });

To address this issue, I added a timeout to ensure that the video plays in the background before fading into view on the page. If anyone has a better idea on how to achieve this, please let me know!

Now, since mobile devices do not require the timeout (as only a background image is displayed), I am wondering if there is a way to conditionally run the timeout based on the device type - for desktops but not for mobile devices. Is such functionality possible?

Edit: Here is my temporary workaround using the following code:

if(jQuery.browser.mobile)
{   
$(window).load(function() {
    $('.loader').fadeOut(1000);    
    $('.loader-bg').fadeOut(1000);
    });
}
else
{
$(window).load(function() {
    setTimeout(function(){
    $('.loader').fadeOut(1000);    
    $('.loader-bg').fadeOut(1000);
    },3000)
    });
}

I believe there may be a more efficient solution out there...

Answer №1

If you're looking for the best way to set a picture as a background until your video starts playing in mb YTPlayer, using a CSS snippet is the way to go. Check out this helpful article for more information.

Learn how to load a picture as background until the video starts in mb YTPlayer

I've personally found this method to be very effective every time. Give it a try and see if it works for you too!

Answer №2

To determine the display resolution, use the following code:

window.screen.availHeight && window.screen.availWidth

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

Data is not found in req.body when making a fetch request

I have been attempting to send a fetch request to my server, but I am consistently receiving an empty req.body. Here is the client-side script: const form = document.getElementById('form1') form.addEventListener('submit', (e) => ...

Unlocking bundles of worth through javascript coding

Is there a way to retrieve a set of values using javascript? Upon page load, I am looking to display an alert showing the value '0-1' for any checked checkboxes. View example here var checkBoxes = document.getElementsByName('mailId[]&apos ...

Difficulty with Nuxt + Vuex: Retrieving data from state using getter

Can anyone assist me with this issue? I am having trouble with my getters in Vuex not recognizing the state. Here is the code snippet: https://codesandbox.io/s/crazy-moon-35fiz?file=/store/user.js user.js: export const state = () => ({ user: { is ...

Turning off CSS on a Wordpress page

I need some assistance with WordPress and CSS. By default, WordPress applies CSS to all posts and pages. However, I want to disable specific CSS styles for certain objects on the page. For example: When I insert a table, it automatically inherits the CSS ...

Instead of logging the JSON file in the console, download it using $.getJson()

Is there a method to download a json file without using jQuery's $.getJSON() and having to log the callback function's argument? I would like to avoid manually typing it from the console.log due to its length. Is there an option to print it, eve ...

Creating a Circular Design with a Centered Line and Text Above and Below using Qualtrics (HTML/CSS/Java)

My knowledge of creating shapes and using these programming languages is limited. I am currently working on a survey in Qualtrics and I need to insert text into circular shapes. Creating one circle was straightforward, thanks to some helpful discussions ...

Phonegap and the Importance of HTTP Requests

Having an unusual issue with Phonegap where my XMLHttpRequests are firing twice. Currently, I am working on an app that involves using XMLHttpRequests to generate a dynamic list of events for users to choose from. In addition to this, I am utilizing jQuery ...

How can you exclude selected values in React-select?

Here is how I have defined my select component: this.state.list = [{label: "test1", value:1}, {label:"test2", value:2}, {label:"test3", value:3}] this.state.selected = [{label:"test2", value:2}] let listMap = this.state.list let list = this.state.list { ...

Display all active ajax/http requests using Javascript or JQuery

Currently, I am constructing a test suite with Selenium. In case of a failed test, I require the ability to determine which vanilla http requests and ajax requests are still in progress. Is there a method to interrogate the browser directly and retrieve a ...

Display One Div at a Time in Sequence on Page Load Using jQuery

I have come across this question multiple times: How to Fade In images on page load using JavaScript one after the other? Fade in divs sequentially Using jQuery .fadeIn() on page load? Despite trying various recommended methods, I'm still unable t ...

The Next Js 13 API route is failing to retrieve accurate data following a post request

I've been working on a project involving authentication using Django backend and Next.js frontend. For state management, I'm utilizing Redux Toolkit. However, I've encountered an issue with the Next.js API that communicates data to the backe ...

How can we determine if the return value from Object.values is an array or not, since it returns a JavaScript array without the brackets?

In my programming code, I work with two separate JSON files. I iterate through each item in the first file and compare its values with those in the second file. Based on the comparison results, I generate a third JSON file which essentially merges the cont ...

Exploring the depths of jQuery promises

My journey into the world of promises has just begun, and I must say it's been quite intriguing. However, there are some questions that have been lingering in my mind. It seems to me that $.Deferred().promise, $.get().promise, and $.fn.promise().pro ...

jQuery Validate not triggering remote action method

I am currently working with ASP.NET MVC4, jQuery Validate, and jQuery Validate unobtrusive. Among the elements on my form, there is only one element that requires validation. The rest of the elements do not need to be validated. Specifically, I have a tex ...

Clicking the checkbox does not synchronize with the table row

Is there a way to activate the button when the user selects the checkbox directly? Currently, the button only becomes enabled when the user clicks on a table row that is clickable. var btn = $("#btn"); btn.attr("disabled","disabled"); $("#table tbo ...

updating a hyperlink attribute dynamically using jQuery ajax

Currently, I have a URL that I am passing to jQuery AJAX. <a href="/wishlist.php?sku=C5&amp;action=move&amp;qty=1" class="buttoncart black">Move To Wishlist</a>; However, when the request reaches the AJAX function, I want to change th ...

Positioning of content in bootstrap's navbar and dropdown menus

The issue lies in the drop-down functionality (you can check out the problematic html code on this codepen, originally inspired by this source). <header class="pb-3"> <nav class="navbar navbar-expand-md navbar-light bg-white borde ...

The G/L account specified in the SAP B1 Service Layer is invalid and cannot be used

I attempted to generate a fresh Incoming payment utilizing the service layer, but encountered this issue G/L account is not valid [PaymentAccounts.AccountCode][line: 1] Here is my JSON: { "DocType": "rAccount", "DueDate& ...

Retrieve both the keys and values from a deeply nested JSON object

My goal is to retrieve JSON data with a specific structure as shown below: {"Points": {"90": {"0": {"name": "John Phillip", "slug": "john"}, {"1&q ...

Guide on how to store a CSS image in a server using PHP

Looking to create a button generator using JavaScript on my website, similar to the one found here http://css-tricks.com/examples/ButtonMaker/. In addition to generating buttons, I also want to include a save button so that users can save the button image ...