Adjusting the time intervals for each slide in real-time

I have a project where I need to adjust the time interval for each individual slide in my carousel in real-time using basic form inputs.

<input type="number" id="slide1">

I found a helpful link on how to achieve this: individual data intervals bootstrap carousel 4

$.fn.carousel.Constructor.prototype.cycle = function (event)
{
    if (!event)
        this._isPaused = false;

    if (this._interval)
    {
        clearInterval(this._interval);
        this._interval = null;
    }

    if (this._config.interval && !this._isPaused)
    {
        var item = $('.carousel-item-next');
        var newInterval = item.data('interval') || this._config.interval;

        this._interval = setInterval(
        (document.visibilityState ? this.nextWhenVisible : this.next).bind(this),
        newInterval);
     }
};

My carousel is now working with different intervals for each slide, which is great.

However, I am facing an issue with changing intervals after the carousel has been initialized. Any suggestions or solutions would be greatly appreciated.

Thank you!

EDIT

Update: The problem was due to my outdated version of Bootstrap...

Answer №1

There is a way to adjust the timing using the data-interval tag attribute. Here is an example snippet:

function formCheck(e) {
  e.preventDefault;
  let $slide1 = $("#durSlide1").val();
  if ($slide1 > 0) {
    $("#slide1").attr("data-interval", $slide1 * 1000);
  } else {
    $("#slide1").removeAttr("data-interval");
  }
  let $slide2 = $("#durSlide2").val();
  if ($slide2 > 0) {
    $("#slide2").attr("data-interval", $slide2 * 1000);
  } else {
    $("#slide2").removeAttr("data-interval");
  }
  let $slide3 = $("#durSlide3").val();
  if ($slide3 > 0) {
    $("#slide3").attr("data-interval", $slide3 * 1000);
  } else {
    $("#slide3").removeAttr("data-interval");
  }
  $(".carousel").carousel("next");
}
<head>
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>

<body>
  <form>
    <div class="form-row mx-3">
      <div class="column">
        <input id="durSlide1" name="durSlide1" class="form-control form-control-sm" placeholder="Duration for Slide #1">
      </div>
      <div class="column">
        <input id="durSlide2" name="durSlide2" class="form-control form-control-sm" placeholder="Duration for Slide #2">
      </div>
      <div class="column">
        <input id="durSlide3" name="durSlide3" class="form-control form-control-sm" placeholder="Duration for Slide #3">
      </div>
      <button type="button" onclick="formCheck(event)" class="btn btn-sm btn-primary">Submit</button>
    </div>
  </form>
  <div id="carouselExampleInterval" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
      <div id="slide1" class="carousel-item active" data-interval="2000">
        <svg class="bd-placeholder-img bd-placeholder-img-lg d-block w-100" width="800" height="150" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img">
        <title>Placeholder</title>
        <rect width="100%" height="100%" fill="#777"></rect>
        <text x="50%" y="50%" fill="#fff" dy=".3em">First slide</text></svg>
      </div>
      <div id="slide2" class="carousel-item" data-interval="2000">
        <svg class="bd-placeholder-img bd-placeholder-img-lg d-block w-100" width="800" height="150" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img">
        <title>Placeholder</title>
        <rect width="100%" height="100%" fill="#777"></rect>
        <text x="50%" y="50%" fill="#fff" dy=".3em">Second slide</text></svg>
      </div>
      <div id="slide3" class="carousel-item" data-interval="2000">
        <svg class="bd-placeholder-img bd-placeholder-img-lg d-block w-100" width="800" height="150" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img">
        <rect width="100%" height="100%" fill="#777"></rect>
        <text x="50%" y="50%" fill="#fff" dy=".3em">Third slide</text>
        </svg>
      </div>
    </div>
    <a class="carousel-control-prev" href="#carouselExampleInterval" role="button" data-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#carouselExampleInterval" role="button" data-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</body>

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

Passing a state object in a POST request body, only to find it arriving empty at the Express server

I'm having an issue with my React form component when making a POST request. The body of the request is showing up as {} even though I'm trying to send my State object. Within my form Component, I am passing the state information to another Reac ...

Bower fails to add components during installation

For my current project, I have been utilizing bower locally to handle JavaScript dependencies with no issues. However, today I encountered a problem when attempting to initiate a new project by reusing code from previous projects. Specifically, I tried to ...

Import objects into THREE.js using OBJLoader, convert to Geometry, apply transformations, and finally convert to BufferGeometry

Trying to figure out why the geometry loaded with OBJLoader cannot be smoothly shaded. var loader = new THREE.OBJLoader(manager); loader.load('/manmodel/js/man.obj', function (object, materials) { console.log(object); console.log(materia ...

Exploring Opencascade.js: Uncovering the Real Text within a TCollection_ExtendedString

I am currently attempting to retrieve the name of an assembly part that I have extracted from a .step file. My method is inspired by a blog post found at , however, I am implementing it using javascript. I have managed to extract the TDataStd_Name attribut ...

Button Type Comparison: Click Event vs Submit Event

Having trouble getting a simple submit button to trigger the click and submit event... html <input type="submit" id="test"> JQuery: $(document).ready(function(){ $('#test').onclick(function(){ alert('Hi') }) $ ...

Expanding/Collapsing Indicator Stays in Place as Content Expands

I'm encountering a problem with my expand/collapse code. I want the p class="heading" tag in the HTML (which is responsible for toggling my expand/collapse box) to move down along with the expanding content. At the moment, the content expands and appe ...

Maxlength and Minlength attributes are not considered when using input type=“number”

Why is the maxlength attribute not functioning properly for this input element? <input type="number" maxlength="5" maxlength="10" /> ...

Is there a way to transform a regular timestamp into a BSON timestamp format?

I am currently utilizing the https://www.npmjs.com/package/mongodb package. Upon inserting my timestamp, mongodb converts it into BSON format (which is expected). The original timestamp value is 641912491, but in the database it appears as 1457904327000. ...

select dropdown in bootstrap is experiencing issues with onchange functionality

<div class="col-lg-6 col-md-6 col-sm-12 m0 col-xs-12"> <div class="select-box"> <select class="select-menu" name="guests" onChange="updateGuestCount(this.value);"> ...

Display an AJAX div when the image is hovered over and have it track the movement of

I am seeking assistance with my website project, which involves providing users with download links to movies. However, I am struggling to make the preview_block div(id) appear when the mouse hovers over the movie_block div(id). Additionally, I am having ...

Waiting for the result of an AngularJS promise

When I click a button in my AngularJS app, the following code is executed: if (!$scope.isChecked) { $scope.getExistingName($scope.userName).then(function (data) { $scope.userName = data; }); } // Additional processing code foll ...

The Veux Store is throwing an error message that says "Array is

When retrieving data from the Vuex Store, I start by checking if the array is present. Following that, my next step is to verify whether the noProducts object at index 0 exists. This validation process is important because the tweakwiseSortedProducts vari ...

Safari is not properly handling element IDs when used in conjunction with React

I am currently in the process of building a straightforward single-page website utilizing React. At the top of the page, there is a navigation bar that contains links to various sections of the site: <li><a href="/#about">About u ...

Maintaining text color while adding a color overlay to a Bootstrap Jumbotron

After searching extensively, I couldn't find any mention of this particular issue. According to the Bootstrap 4.2 documentation on the Jumbotron, I constructed my div with an inline background image. However, when I attempted to apply an overlay on to ...

AngularJS showcases the full spectrum of available methods

I am struggling to create a method for composing a URL in my class. Here is what I have attempted: The createLink method takes an ID and returns the corresponding URL: console.log("before the method"); console.log($scope.createLink ) $scope.createLink = f ...

Tips for resolving issues with dropdown menu placement

Here is the current state of my HTML and CSS code: <div class="header"> <div class="container-fluid"> <div class="row"> <div class="col-1"></div> <div class="col-10"></div> <div class="col-1 dropd ...

Ways to resolve the issue of columns having unequal heights in Bootstrap

I'm a novice when it comes to bootstrap. I'm endeavoring to put together a gallery showcasing some magnificent artwork. However, I've encountered an issue. The columns are all taking the same height even though the images aren't particu ...

Steps to submit a JavaScript-generated output as the value in a form input field

I'm facing an issue that seems basic, but I can't seem to figure it out. I'm trying to create a binary string representing the 12 months of the year using 12 checkboxes: const checkboxes = [...document.querySelectorAll('input[type=check ...

Can one utilize Javascript to write in plain text format?

Currently, using JavaScript I have a plain text containing data that is displayed within my form tags. Everything is functioning correctly, but now I need to update the values inside the code of my form tags in order for the changes to also be reflected in ...

Attempting to create a table structure with rows and cells dynamically within CoffeeScript utilizing the Google Closure Library

Struggling to embed a table, rows, and columns into an HTML page using coffeeScript and the google closure library. If coffeeScript doesn't work out, I may resort to jQuery or plain javaScript, so any guidance on that front would be much appreciated. ...