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

Solution for fixing the error: MongooseError [OverwriteModelError]: It is not possible to overwrite the `User` model after it has been compiled in

I am new to working with the MERN stack and currently attempting to create an exercise tracker app following a tutorial on YouTube. However, I am encountering the Mongoose: OverwriteModelError when running the server and cannot seem to identify where I am ...

Using Javascript to apply styling only to the first class element in the document

I am facing a challenge with styling programmatically generated classes and inputs. Here is an example of the structure: <div class="inputHolder"> <input type="button" class="inputclss" value="1"> </ ...

Using AJAX to inject JSON data from PHP into Edge Animate

For a school assignment, I am currently working on a project using edge animate. My objective is to import data from a database hosted on my school's webspace and incorporate it into the edge animate project. Despite researching online for a solution ...

IE9 fails to display li::before element correctly

I have created a utility navigation bar using an unordered list and I am attempting to use li::before to add a pipe "|" symbol between each link in the navigation. Below is my utility navigation... <div class="horizontalnavlinks"> <ul> ...

Calculation Error in JavaScript/JQuery

I've been working on a JavaScript function to calculate the sum of values entered into textboxes, but it seems to be giving me inaccurate results in certain cases. Check out the FIDDLE here Enter values : 234.32 and 32.34 Result: 266.6599999999999 ...

Just starting out with Boostrap; looking for ways to tidy up this code and achieve the intended effect

UPDATE: Check out the revised JS Fiddle link at the end of this post; I managed to achieve the desired result, but I believe there's room for improvement in the code! After experimenting with HTML/CSS for some time, I'm now working on my first l ...

Authenticate through Twitter when using PhoneGap Cordova

Looking to implement Twitter login in my application using JavaScript and HTML. How can I redirect users to the Twitter login page when they click on the "Sign In with Twitter" button? ...

Spring MVC: Incorporating Java Map into Optgroup Options

In my Spring MVC project, I am currently passing a Map from Java to my webpage using the variable "userLocales" through request.setAttribute('userLocales', bluh bluh bluh). I am looking for a way to extract the strings from this variable and use ...

Disable the jquery animation when the mouse leaves and return it to its initial state

My goal is to scale to 2.5 when hovering over the div element. The animation starts on hover, but if I quickly move my cursor out of the div, the entire scaling animation completes before returning to its original size. I want the animation to stop at its ...

Gracefully Switching Between Various Functions

Suppose I have a collection of functions that perform various tasks: function doSomething() { console.log('doing something'); } function accomplishTasks() { console.log('accomplishing tasks'); } function executeAction() { console. ...

Is there a way for me to determine which events are linked to an object?

I am currently utilizing jqGrid for displaying read-only data from http://www.trirand.com/blog/. However, the resizable columns are causing issues with other draggable elements on the page, as they tend to get stuck when dragged over the column resize area ...

Tips on saving additional parameters in an API URL with AngularJS

Here is my AngularJS function code: $scope.cardcall = function (cardtype) { $scope.cityname=cityname; $http({method: 'GET',url: '/api/v1/asasas&filterBy=cardNames&filterByValue='+cardtype.key}).success(funct ...

Utilize IntelliJ's TypeScript/JavaScript feature to extract a method from all instances

I am relatively new to using IntelliJ Idea Ultimate 2020 and I am currently exploring the refactoring functionalities within the software. Is there a way to extract a method from a section of code and apply it to all instances easily and exclusively withi ...

Refresh information periodically within a Node.js application

Recently, I developed a web application using nodejs to fetch data from Amazon. My goal is to have the app update at regular intervals for different purposes: - Every 15 minutes for real-time inventory monitoring - Once daily for less frequently changing d ...

Stop a hacker from obtaining the usernames from a system

Our forgot password page has been identified with a security issue that needs attention: ISS-0003938 Web Inspect Open Medium Suspicious Files Found in Recursive Directory ****** Remove any unnecessary pages from the web server If any files are nec ...

Executing window.open from Html.Actionlink in ASP.NET MVC 4

Here is the code block I am working with: @foreach (System.Data.DataRow drEquipment in Model.EquipmentList.Rows) { <tr> <td valign="top">@drEquipment["ColumnName"]<br /></td> <td valign="to ...

using selenium to interact with elements within the window object

I'm a newcomer to selenium and javascript and looking to incorporate the VisualEvent javascript into pages opened in a selenium-controlled browser. My goal is to access its variables from selenium in java. I've successfully completed the first ph ...

How can I clear my object so that new Dates() can be added to my calendar?

I am working on updating my program to seamlessly replace old JSON data from a holidays API with new data as soon as it is received. Initially, I attempted to declare the array as empty at the start, but this approach did not yield the desired results. Si ...

Uncover the structure of a regular expression pattern to discover the quantity of tokens and the size of the anticipated matches

I am looking to validate certain fields in my forms by determining the number of tokens and length specified by a particular regular expression. For example, For the pattern [0-9]{3},[0-9]{2}, I need to identify 5 as the length and 2 as the number of to ...

Can you explain the purpose of the square brackets within the ".module("modulename", [...])" syntax used in AngularJS?

I recently came across a sample demonstrating routing in AngularJS. I am curious about the connection between the dependency 'ngRoute' and the module mainApp, as shown in the syntax var mainApp = angular.module("mainApp", ['ngRoute']);. ...