The setInterval function appears to only run once in Chrome, but is functioning properly in all other browsers

Check out the slideshow code below:

HTML

<div id="slideshow"><img src="common/images/background/Slide1b.jpg" />
   <img src="common/images/background/Slide2b.jpg" />
   <img src="common/images/background/Slide3b.jpg" />
   <img src="common/images/background/Slide4b.jpg" />
</div>

jQuery

var opt1 = 1;
$(document).ready(function() {

    $('#slideshow').supersize();
    all_images = $('#slideshow > img');
    all_images.hide();

    first = $('#slideshow > img:eq(0)');
    first.show().attr('class', 'power');

    setInterval(function() { 
        var total = $('#slideshow > img').length;

            if(opt1 < total) {
                var current = $('.power');
                var next = $('.power').next();              

                current.removeClass('power').fadeOut('slow');
                next.fadeIn('slow').addClass('power');

                ++opt1;
            } else {
                opt1 = 1;
                all_images.removeClass('power').fadeOut('slow');
                first.addClass('power').fadeIn('slow');             
            }
    }, 2000);       

});

The above code seems to work well on all browsers, however, it has been reported that in Google Chrome it only runs once.

Answer №1

This answer showcases a unique approach to creating a slideshow functionality using JavaScript. It seems that Chrome prefers setTimeout over setInterval for this particular task, and the code utilizes .animate instead of the more traditional .fadeIn and .fadeOut methods.

$(document).ready(function()
{
    $('#slideshow').supersize();
    var images = $('#slideshow>img');
    images.css('opacity', 0)

    var first = images.eq(0);
    first.css('opacity', 1).addClass('power');

    setTimeout(on_timer, 2000);

    function on_timer()
    {
       var current = $('.power');
       var next = $('.power').next();       

       if (next.length == 0)
          next = first;

       current.removeClass('power').animate({opacity: 0}, 1000);
       next.css('opacity', 0).animate({opacity: 1}, 1000).addClass('power');

       setTimeout(on_timer, 2000);
    }
});

Check out the live demo at:

http://62.168.145.82/slideshow/

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

Issue with RefineryCMS nivo slider's functionality when jQuery is required

Currently, I am utilizing RefineryCMS 3.0.0 and have incorporated the nivo gem to create a slideshow on the home page. The require line has been added to my application.js page as follows; //= require jquery //= require jquery_ujs //= require turbolinks ...

Issue with displaying Jquery AJAX response in specific div

Having trouble with my JQUERY function. I've got an onclick event triggering a dialog box to open. But upon opening, the dialog box calls two separate jQuery functions. Both functions are supposed to fetch member information based on ID, but only on ...

The template failed to load on AngularJS post-compilation

Following the build of my Angular app, I encountered this error message: [$compile:tpload] Failed to load template: app/app.html (HTTP status: 404 Not Found). Seeking assistance from you all!!!!! app.html <div class="root"> <div ui-view> ...

Customizing error messages in react-hook-form-mui: A step-by-step guide

I'm currently diving into the world of react-hook-form-mui library. The documentation provided for this project is quite brief, and I've been struggling to add a validation rule to my form field. Despite trying various methods, none seem to be ef ...

JavaScript - Populating Dropdown Menu with Fresh Information

I'm attempting to populate a combobox (input) with data fetched via AJAX. The goal is to display every city associated with a selected state, chosen from another select control (the state control). My Attempt: I've implemented the "change" even ...

Can one integrate various angular components constructed using distinct versions of Angular?

Is it feasible to utilize various angular components (custom elements) created with different versions of Angular? I've heard concerns about zone.js causing global scope pollution. Appreciate your response! ...

What is the best way to use JavaScript to fill in missing images with alternative HTML content?

As a provider of a service that helps users find the location of pictures, I face a challenge due to the fact that the pictures are stored on another internal site. Some users may not have access to this site as my service offers additional information. Th ...

I am looking to integrate my information into the user interface using Angular

import { Component, ViewEncapsulation } from '@angular/core'; import { Router } from '@angular/router'; import { Batch } from '../../../config/batchnew/batch.model'; import { BatchService } from '../../../config/batchnew ...

Having trouble getting my Leaflet map to display even after meticulously following the quick-start guide

I am experiencing an issue where the map is not displaying at all, leaving a blank space where it should be. Despite following Leaflet's quick-start guide, I have been unable to determine the cause of this problem. Here is the code that I currently h ...

Safari browser removes spaces after commas in cookie values

I'm encountering an issue when trying to set a session cookie for storing an Address. It seems that whenever I include a comma followed by a space in the cookie's value, Safari automatically removes the spaces after the commas, causing the format ...

An error is thrown when using AngularJS .length property

Currently, I am carrying out a regular task within my Filter to verify if angular.module('someApp') .filter('filterSomeData',['$filter',function ($filter) { return function (items, keyObj) { var filterObj ...

What is the best method for eliminating a character from all elements in jQuery classes?

I am working on an Angular 4 app where every .inner-page class in a html element includes a "/". For instance: <div _ngcontent-c0="" class="inner-page /login"> <div _ngcontent-c0="" class="inner-page /register"> I need to eliminate the "/" c ...

Global Path in Helmet Content Security Policy is not functioning as expected

I recently implemented Helmet to establish content security policies for my web application using Express in the backend. The specific policies are as follows: const express = require("express"); const app = express(); const helmet = require('helmet&a ...

Determine if an object is already present in a JSON array by comparing their respective IDs

I have a shopping cart stored in JSON format. [{"tuote":{"id":"2","name":"Rengas 2","count":16,"price":"120.00"}},{"tuote":{"id":"1","name":"Rengas 6","count":"4","price":"25.00"}},{"tuote":{"id":"4","name":"Rengas 4","count":"4","price":"85.00"}}] Form ...

The has-error class cannot be applied to certain input elements

I am currently in the process of validating some inputs within a modal: <div id="modal-section" class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-hidden="true"> <div class="modal-dialog modal-lg"> <div class="m ...

Accessing Category Field Value in Mongoose One-to-Many Relationship

I am managing two collections: Category and Book Book: const mongoose = require('mongoose'); // eslint-disable-next-line camelcase const mongoose_delete = require('mongoose-delete'); const { Schem ...

Implementing Google APIs with Next.js: Managing arrays without keys

Recently, I've started using next.js and came across the getStaticProps function. In this function, I'm loading data from a Google Sheet table into a constant. The result is an array of arrays, just like what the Google API returns shown below: [ ...

Should a MEAN stack app require the use of two servers in order to run?

Currently, I am in the process of developing a MEAN stack application which involves using MongoDB, ExpressJs, Angular 6, and NodeJs. One issue I am facing is determining whether two servers will be necessary to run the app simultaneously. Specifically, o ...

Encountering a 405 error when attempting to send a request through an express route in a Next

After deploying my Express js routes in Next js on hosting, I encountered an error 405 when sending requests to the route. However, everything works fine when I test it on localhost. I'm puzzled by this issue. Can anyone help me understand what' ...

Utilizing NodeJS and Express to enhance the client side for showcasing an uploaded file

My knowledge of nodeJS, AJAX requests, and routing is still in its infancy. After following a tutorial on nodejs and express examples, I managed to get everything working on the server-side. However, I'm facing difficulty displaying the uploaded file ...