Interval function not initiating properly post bullet navigation activation

Currently, I am experiencing an issue with my custom slider where the auto sliding set interval function is not working after using the bullet navigation. Despite trying to implement "setTimeout(autoSlide, 1000);", it doesn't seem to be resolving the problem. Below is the code snippet that I have been working on. Any assistance or solution provided would be greatly appreciated. Thank you.

<!doctype html>
<head>
    <title> Custom Slider </title>
    <style type="text/css">
        body
        {
            margin: 0;
            padding: 0;
        }
        .slide
        {
            float: left;
            width: 960px;
            height: 300px;
            background-color: #000;
        }
        .slide h1
        {
            color: #fff;
        }
        #container
        {
            width: 960px;
            overflow: hidden;
            margin: auto;
            margin-top: 100px;
        }
        #wrapper
        {
            position: relative;
            right: 0px;
        }
        #bullets li
        {
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id="container">
        <div id="wrapper">
            <div class="slide">
                <h1>Slide 1</h1>
            </div>
            <div class="slide">
                <h1>Slide 2</h1>
            </div>
            <div class="slide">
                <h1>Slide 3</h1>
            </div>
            <div class="slide">
                <h1>Slide 4</h1>
            </div>
            <div style="clear: both;"> </div>
        </div>
    </div>
    <ol id="bullets">
    </ol>
    <script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var slideWidth = $('.slide').width();
            var slideLength = $('.slide').length;
            var totalWidth = slideWidth * slideLength;
            $('#wrapper').css('width', totalWidth);
            var currentPos = $('#wrapper').position().right;
            var currentIndex = 0;
            var autoSlide;
            function slideAuto(){
                currentIndex += 1;
                if(currentIndex > slideLength - 1)
                {
                    currentIndex = 0;
                }
                $('#wrapper').animate({right: currentIndex * slideWidth});
            }

            var autoSlide = setInterval(slideAuto, 1000);
            $('.slide').each(function(){
                $('#bullets').append('<li class="bullet"> </li>');
            });

            $('.bullet').click(function(){
                clearInterval(autoSlide);
                var bulletIndex = $(this).index();
                if(bulletIndex > slideLength - 1)
                {
                    bulletIndex = 0;
                }           
                $('#wrapper').animate({right: bulletIndex * slideWidth});       
                currentIndex = bulletIndex;
                setTimeout(autoSlide, 1000);
            });
        });
    </script>
</body>

Answer №1

setTimeout(autoSlide, 1000);

The code above is attempting to pass an old timer handle into setInterval, which will not work correctly. To fix this issue, the function needs to be passed again:

autoSlide = setTimeout(auto, 1000);

It's important to note that setTimeout creates a timer that only fires once, while setInterval sets up a repeating timer. If you intended for the timer to repeat, you should use setInterval instead.

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

Need assistance with debugging the current solution for the todo App using Commander or considering a different approach with Readline and Event Emitter?

I'm currently working on developing a CLI for a Node.js exclusive todo application using the commander and conf modules within Node.js, along with chalk to add color to the output. I've encountered some issues that I'm unsure how to resolve: ...

Using jQuery to interact with newly added DOM elements

My task involves creating a dynamic form to load items from a database. When the user clicks the submit button, the DOM will append new elements displaying each item's name, quantity, and an input field for updating the quantity. <input type="text ...

Rails Unobtrusive JavaScript (UJS) does not seem to be connecting the data-remote

Version of Rails: 3.2.1 Version of Ruby: 1.9.3p125 Browser Used: Chrome 18.0.1025.162 Developers Operating System: Mac OS/X Lion Server's Operating System: CentOS 5 I am attempting to utilize :remote in my link_to call in order to request HTML co ...

What should be transmitted to the front-end following the successful validation of a token on the server?

My process starts with a login page and then moves to the profile page. When it comes to handling the token on the backend, I use the following code: app.use(verifyToken); function verifyToken(req, res, next) { if (req.path === '/auth/google&ap ...

What mistakes am I making in this PHP code as I try to work with the select option?

Currently, I am developing a form that involves selecting values. If the user chooses 'yes', I want to display a text box section. However, the code below is not functioning as expected. <select id="gap" name="gap" onclick="gap_textbox();"> ...

When referencing a particular React commit in package.json, it may result in the installation of react-tools instead of react itself

After including the following line in my package.json: "react": "git://github.com/facebook/react.git#08e4420019f74b7c93e64f59c443970359102530" When I execute npm install, I notice that node_modules/react-tools has been installed instead of node_modules/r ...

Incomplete Json information

As I embark on my journey to learn Javascript and work on building an application simultaneously, I can't help but feel optimistic about the learning process. To guide me through this venture, I've turned to the teachings of Alex MacCaw in his bo ...

When using a variable to fetch data in JSON, an undefined error occurs. However, if a hardcoded index

I am facing an issue while trying to extract and manipulate JSON data from a file for an application I am developing. When looping through the data, I encounter an undefined error that seems to indicate a missing property in the JSON object when accessing ...

The position of the parent element's bounding box in relation to the child element

I've been mulling over this question for quite some time now, and I finally decided to bring it up. Hopefully, it's straightforward and not something that has been addressed before... Here's the scenario: I have a list of items with anchor ...

Vue app hosted on Firebase displays a blank page when user logs in

After deploying my Vue2 project to Firebase hosting server, visitors are required to log in to access the other pages. The issue is that once a user successfully logs in, they are redirected to the next page but it appears blank. Below is what the firebas ...

Discovering the current page URL in jQuery Mobile

I am struggling to retrieve the current page URL in a jQuery Mobile website (using the default ajax navigation). My latest attempt involves binding to the `pagecreate` event, but unfortunately it does not seem to work for any pages that are injected into ...

In what way can you establish a boundary for a border?

My goal is to create a 10x10 grid with randomly placed black boxes, but I am facing an issue in my game setup: Currently, the 5 black boxes are generated randomly in a row, sometimes exceeding the border and breaking the continuity. I would like to establ ...

What is the best way to navigate between elements using AJAX?

My experience in Ajax is minimal. I've managed to create two <li> elements with <a> elements inside, each linking to different HTML files. My goal is for clicking on a link to automatically load the corresponding HTML file without refreshi ...

React JS error: Trying to use props.video as a function, but it's not

As a newcomer to React, I'm encountering some errors that I need help debugging. Error #1: Uncaught TypeError: props.videos.map is not a function at new VideoList Error #2: bundle.js:19956 Error: findComponentRoot(..., .0.0.0): Unable to find el ...

Troubleshooting Problem with Padding in ion-content for Ionic Angular

I am currently developing my inaugural Ionic application. The initial template I utilized was the rudimentary sidemenu layout. $ ionic start myApp sidemenu Afterwards, I crafted a straightforward page featuring a list which appears as follows... <ion ...

Node Express for Advanced Routing

I'm currently developing a web application that will handle CRUD operations on an array within a collection. Here is the structure of the collection model: var mongoose = require('mongoose'); var website = require('./website'); ...

What is the best way to extract the value from a resolved Promise?

Currently, I am attempting to read a file uploaded by the user and convert it into a String using two functions. The first function is handleFileInput: handleFileInput(event){ setTimeOut(async()=>{ let abcd= await this.convertFileToString(this.fi ...

The setTimeout functionality is executing faster than expected

In my selenium test, I've noticed that the setTimeout function consistently finishes about 25% faster than it should. For example, when waiting for 20 seconds, the function completes after only 15 seconds. test.describe('basic login test',f ...

What methods are available to test my website across different browsers such as outdated versions of Internet Explorer like IE8?

Currently, I am working on Chrome with Windows 7 OS installed. However, Windows 7 does not support Internet Explorer 8. This is causing issues when trying to view my web pages in the IE8 version and older. How can I resolve this problem? I have attempted ...

DIV size issue resolved

Content within a div is surpassing the set fixed size determined by CSS. <style> #fixeddiv { position: fixed; margin: auto; max-height: 300px; max-width: 700px; } </style> <div id="fixeddiv"> <div id="M77 ...