Delay loading background image until a specific time has passed, preventing website from fully loading

My website features a vibrant backgroundImage Slideshow that functions seamlessly.

However, I am looking to reload the images of the slideshow after a specific time interval. The issue is that the website keeps loading endlessly.

Once the website has completed loading, a script (slideshow.js) should be activated in the background to load the images.

I have experimented with various methods such as

  • <body onload="...">
  • $(document).ready(function(){...});
  • <script>startSlideshow()</script> (placed at the end of html)

Unfortunately, all these attempts resulted in the page continuing to load until all pictures were fully loaded.

Any suggestions or tips that could point me towards a solution would be greatly appreciated.


index.php

...

<!-- slideshow-->
<div class="bg-slideshow" title="Background Image">
  <ul class="slideshow">
    <li><span>Image 01</span></li>
    <li><span>Image 02</span></li>
    <li><span>Image 03</span></li>
    <li><span>Image 04</span></li>
    <li><span>Image 05</span></li>
  </ul>
</div>

...

slideshow.js

function startSlideshow() {
  var parent = document.getElementsByClassName("slideshow");
  var child = parent[0].querySelectorAll("span");

  for (var i = 1; i < 5; i++) {
    child[i].style.backgroundImage = 'url("images/slideshow/p' + (i + 1) + '.jpg")';
    /*Wait 2000ms, delay*/
    sleep(2000);
  }
}

function sleep(){...}

style.css

.slideshow li:nth-child(1) span {
  background-image: url("../images/slideshow/p1.jpg");
}

.slideshow li:nth-child(2) span {
  /*placeholder (replace by javascript)*/
  background-image: url("../images/foto-404.jpg");
  /*[...animation delay...]*/
}

Answer №1

Here is my solution:

setTimeout(function() {
  console.log("Images loaded");
  var parent = document.getElementsByClassName("slideshow");
  var child = parent[0].querySelectorAll("span");

  for (var i = 1; i < 5; i++) {
    child[i].style.backgroundImage = 'url("images/slideshow/p' + (i + 1) + '.jpg")';
  }
}, 10000);

The page loads normally. When including my .js file with this code, a timer of 10 seconds starts. After the 10-second delay, the functions kick in and replace my image files.

With some additional effort, I can load my images gradually and optimize network data usage.

Check out Google Chrome Network stats here

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

Learn how to apply formatting to all textboxes on an ASP.NET website using CSS without having to set the CSS class property for each individual textbox

I am currently developing a website using Asp.Net. I am looking for a way to customize the font, color, and background color of all the textboxes on my site using CSS. However, I would like to avoid having to assign a "cssclass" property to each individua ...

Generate visual representations of data sorted by category using AngularJS components

I am facing an unusual issue with Highcharts and Angularjs 1.6 integration. I have implemented components to display graphs based on the chart type. Below is an example of the JSON data structure: "Widgets":[ { "Id":1, "description":"Tes ...

"Adaptable design that seamlessly adjusts to all types of content

I am currently faced with the task of developing a highly adaptable layout for a content management system, one that adjusts automatically based on the content provided. The initial layout is set to a minimum width of 1024px and is functional in most cases ...

Incorporate a Fadein effect into the current CSS for mouseover link interaction

Is there a way to enhance my current css code for a mouseover link with a background image by adding a fade in and out effect? What's the most effective method to achieve this using jquery? .sendB { width: 100%; height: 125px; background: ...

Is it possible to share a Vue.js component by "transferring" it rather than duplicating it?

In my comment system project using Vue.js, I have over 300 comments to manage. Each comment has an admin section that appears when the cursor hovers over it. Duplicating the admin section component for each comment is not ideal as it creates unnecessary l ...

Arranging an image and button within a column div row using Bootstrap

I'm encountering an issue with Bootstrap where I am trying to align the blue buttons neatly below the image. Link to the image: img Here is a visual representation: https://i.sstatic.net/tB5mU.png This is the code snippet: <div class="row&q ...

Using only python, launch a local html file on localhost

Currently, I am executing a code on a remote server that periodically creates an "status" HTML file (similar to TensorBoard) and saves it in a directory on the server. To check the status, I download this HTML file whenever needed. However, if I could use ...

Challenges with incorporating numerous text boxes in real time

There are two text boxes side by side with a "+" sign next to each. When the plus sign is clicked, a new text box appears with both the "+" and "-" signs for adding and removing the text box. I followed instructions from this resource to create my text box ...

Update image attributes (such as src, alt, etc.) after a successful AJAX request (and also help me troubleshoot my AJAX

The image link I am currently using is: echo "<br/><div class='right' id='post" . $id . "'> <img id='thumb' src='/images/like.png' title='Like it?' alt='Like button' onC ...

Populate an HTML5 arc with an image - Leveraging the power of HTML5 Canvas

I'm working with an HTML5 Canvas (JSFiddle) that currently displays circles created using the following function: function createball(x,y,r,color){ context.beginPath(); context.arc(x,y,r,0,Math.PI*2,true); context.fillStyle = color; c ...

Refreshing a Next.js website on-demand using the Django model's save function

My current setup involves a next.js website deployed on Vercel, which retrieves data from an API provided by Django. Utilizing a new feature in Next.js known as Incremental Static Regeneration On-Demand, I am able to rebuild specific pages without having t ...

Using Formik: When the initial value is changed, the props.value will be updated

After the initial props are updated, it is important to also update the values in the forms accordingly. export default withFormik({ mapPropsToValues: (props: Props) => { return ( { id: props.initialData.id, ...

Retrieving data from a script within a React component

How can I access a variable from a script inside a React component? I am performing device detection on Node and sending the resulting object to the client (React) within index.ejs. <script type="text/javascript">window.deviceType = <%- deviceT ...

Positioning divs around a circle can be quite challenging

Among my collection of divs with various classes and multiple child divs representing guests at a table, each main div symbolizes a specific restaurant table type. I have created a jsfiddle for demonstration. http://jsfiddle.net/rkqBD/ In the provided ex ...

What is the best way to combine a top <div> with a bottom <div> in an image without creating unsightly white gaps?

I need help overlapping two divs on top of an image in HTML. The first div should cover part of the top area of the image, while the second div should cover part of the bottom area of the image. These divs should be displayed over the image without leaving ...

What is the best method for securing my API key within the script.js file while utilizing dotenv?

My goal is to conceal my API key using dotenv. Interestingly, when I replace require('dotenv').config(); with my actual API key in the code as apiKey: process.env.API_KEY, it works fine despite not using process.env.API_KEY. I made sure to inst ...

The Jquery ajax function is functional on a local environment, but fails to work when

Can someone please help me with this issue? I've been trying all day to figure it out, but I can't seem to... So, I have this function: function doSearch(){ $('#dg').datagrid('load',{ search: $('#sea ...

Infinite Scrolling in React: Troubleshooting Issues with the dataLength Parameter

A function called newsFeed is utilized within the useEffect hook to make a request to an endpoint for fetching posts made by the logged in user and users they follow. The retrieved data is then saved to the state: const [posts, setPosts] = useState([]) con ...

Modify the scrollbar's width, color, and corner radius to customize its appearance

Here is the table code where the styles are applied. By setting the tablecontainer height to 600px, we can make the table scrollable. However, I would like to customize the scrollbar with a different color, width, and corner radius. <TableContainer s ...

AngularJS - Viewless and Issue-Free

I'm currently working on a project that involves using AngularJS and PHP. I made some changes, but now it's not functioning properly... The issue is that there are no errors in the console, Angular works (I can retrieve its version), but my vi ...