Leveraging JQuery animation to manipulate the spinning of HTML jackpot

I have been using the following code to simulate a spinning wheel for users. It is functional, but I am facing an issue where the rotation stops abruptly at a specific circle before applying the style for the winning circle.

My query is: Is there any way to slow down the spin rotation and ensure that it lands on the winner smoothly, rather than jumping to the winning number after stopping? I want the wheel to continue spinning until it naturally falls on the winning number.

$(function() {
  
      var current = 1;
      var num = 0;
      var max = $('.jackpot').length + 1;
      var rotate = 5;
      var speed = 80;
  
      function change() {
        var winner = Math.floor((Math.random() * 12) + 1);
        $('.jack-' + current).toggleClass("active-spin spin-default");
        current++;
        if (current == max) {
          current = 1;
          num++;
        }
        if (num == 3) {
          speed = 160;
        }
  
        if (num != rotate) {
          setTimeout(function() {
            change()
          }, speed);
        } else {
          $('.jackpot').removeClass("active-spin").addClass("spin-default");
          $('.jack-' + winner).addClass("iswin");
          current = winner;
          num = 0;
        }
      }
  
      $('.play').click(function(event) {
        $('.jackpot').removeClass("iswin").addClass('spin-default');
        setTimeout(function() {
          change()
        }, speed);
      });
  });
.jackpot {
      display: inline-block;
      padding: 10px;
      margin: 10px;
      border-radius: 50%;
      border: 1px solid transparent;
      transition: 0.5s;
    }
    
    .spin-default {
      border: 1px solid orange;
      background-color: #eee;
    }
    
    .jackpot.active-spin {
      border: 1px solid blue;
      background-color: red;
    }
    
    .jackpot.iswin {
      border: 1px solid yellow;
      background-color: green;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div>
      <table>
        <tr>
          <td>
            <div class="jackpot spin-default jack-1">JK</div>
          ...
            </div>
          </td>
        </tr>
      </table>

    </div>
    <button class="play">PLay</button>

Answer №1

Here is a solution for you to try: The lastRound() function is what you need. Your code for determining the winner is in the findWinner() function within another part of the program.

$(function() {

  var current = 1;
  var num = 0;
  var max = $('.jackpot').length + 1;
  var rotate = 5;
  var speed = 80;
  var winner = 0;

  function change() {
    winner = Math.floor((Math.random() * 12) + 1);
    $('.jack-' + current).toggleClass("active-spin spin-default");
    current++;
    if (current == max) {
      current = 1;
      num++;
    }
    if (num == 3) {
      speed = 160;
    }

    if (num != rotate) {
      setTimeout(function() {
        change()
      }, speed);
    } else {
      current = 1;
     lastRound();
    }
  }

  function lastRound(){

     setTimeout(function() {
       $('.jack-' + current).toggleClass("active-spin spin-default");
       if (current == winner){
         findWinner();
       }else{
         current++;
         lastRound();
       }
      }, speed + current * 20);
    }


     function findWinner(){
     $('.jackpot').removeClass("active-spin").addClass("spin-default");
      $('.jack-' + winner).addClass("iswin");
      current = winner;
      num = 0;
  }

  $('.play').click(function(event) {
    $('.jackpot').removeClass("iswin").addClass('spin-default');
    setTimeout(function() {
      change()
    }, speed);
  });
});

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

I am utilizing Vuex to store all of the product details and handle API request queries efficiently

Question regarding Vue/Vuex efficiency and best practices. I am working with an API that returns a paginated data-set of 50 products, which I am storing in a Vuex store. When displaying all products, I iterate over the Vuex store. Now, for individual pr ...

What is the best way to change a Buffer array into hexadecimal format?

After making a call to one of my API endpoints, I am receiving a Buffer array in a JSON object. My goal is to convert this array into a more user-friendly format such as hex so that I can easily compare them. Below is a snippet of the current object struct ...

Iterate over div elements using jQuery

I am experimenting with creating a jQuery slider using divs: <div id="block-1"> <div id="bannerleft"> <div class="wsite-header-1"></div> </div> <div id="bannerright"> < ...

Storing JSON data retrieved from a fetch API request in a JavaScript global variable - a beginner's guide

I have been experimenting with the fetch API and successfully managed to log the fetched data to the console using the then() method. However, I am struggling to store this data in a global variable for later use in vanilla javascript due to the nature of ...

Filter jQuery search results for classes with identical names

I am new to using jQuery, so please excuse my lack of experience. My current challenge involves 'getting a reference to an object wrapped in a class', but there are multiple classes with the same name. How can I specifically target and retrieve t ...

Navigate to a specific element using Selenium WebDriver in Java

Currently, I am utilizing Selenium along with Java and ChromeDriver to execute a few scripts on a website. My goal is to scroll the driver or the page to a specific element positioned on the webpage. It is important that this element is visible. I am awa ...

Are there any JavaScript functions available that can navigate to a different HTML page?

Here is an example of the functionality I am attempting. Can it be implemented? function CloseHTML(){ ApplyCSSClosingTransition(); setTimeout(function() { RedirectToAnotherPage(); }, 2000); } <div onClick='CloseHTML()'&g ...

``CSS: Styling a Rounded Div with a Clipped Scrollbar

I am attempting to contain a scrollbar within a div so that it remains within the rounded corners without overflowing. Take a look at the code snippet below to see the problem in action. Is there a way for the scrollbar to be fixed in its position, while ...

Having difficulty positioning the dropdown above the other elements in the body

Below, you'll notice that the dropdown menu isn't positioned correctly over other body elements like the timer. I'm utilizing bootstrap for the dropdown and redcountdown js for the timer. https://i.sstatic.net/OO8Jm.png Here is the HTML: & ...

alternative for in jQuery version 2

Having some issues with the "on" function in jQuery 2.0.2 when working with dynamically added elements. Below is an example where the second part seems to be failing to work: <button class="btn btn-danger btn-lg" id="hi">Danger</button> ...

Optimizing rest service calls with $resource

I am currently learning about the $resource to utilize RESTful Web Services. For my first attempt, I created a factory like this : 'use strict'; angular.module('BalrogApp').factory('Req', ['$resource', function($r ...

What is the best approach in JavaScript (jQuery) to loop through every 'a' element on a webpage, sequentially applying a unique style to each one?

Imagine this scenario: On a webpage, there are multiple 'a' tags with different alt tag values: <a href="img1.jpg" class="myClass" alt="0,0,600,200"></a> <a href="img2.jpg" class="myClass" alt="200,0,600,75"></a> <a hr ...

Declaring an array that holds Angular components in Angular 11: What's the best approach?

I'm trying to implement a feature in my Angular component where I can create a list that displays content from other components. My current approach involves declaring an array that contains references to different Angular components: import { Compone ...

Customizing the Class of a jQuery UI ui-autocomplete Combobox Container

Is there a way to customize the ui-autocomplete div by adding a custom class? I have several autocomplete widgets on my webpage, and I need to style their drop-downs differently. Since editing the ui-autocomplete class directly is not an option, I am wor ...

Serve as a proxy for several hosts with identical URL structures

I've been utilizing the http-proxy-middleware to handle my API calls. Is there a way to proxy multiple target hosts? I've searched for solutions in the issues but still haven't found a clear answer. https://github.com/chimurai/http-proxy-m ...

Dropdown menu in Bootstrap gets cropped behind a scrollable container

I am encountering a problem with my Bootstrap Drop Down. The dropdown is located within a scrollable div with a fixed height. Whenever I click on the dropdown, the menu appears behind the scroll content. However, a normal select works perfectly fine. htt ...

How can images from a dynamic table be converted to base64 format in Vue.js and then sent in a JSON file?

Hello everyone, I'm facing an issue with a dynamic table where I need to add multiple rows, each containing a different image. I attempted to convert these images to base64 format and save them in a JSON file along with the table data. However, the pr ...

Filtering in JavaScript can be efficiently done by checking for multiple values and only including them if they are not

In my current coding challenge, I am attempting to create a filter that considers multiple values and only acts on them if they are not empty. Take the following example: jobsTracked = [ { "company": "Company1", ...

Is it possible to target li a instead of li in jQuery and CSS?

Currently in the process of integrating bootstraps scrollspy on my website, while also working with a menu similar to this example: At the moment, I am able to target the li in the navigation, but what I really want is to target the li a. selector = ...

Uncertain about the distinction between reducers and dispatchers when it comes to handling actions

I'm feeling a bit confused regarding reducers and dispatchers. While both receive actions as parameters, it doesn't necessarily mean that the actions I use in my dispatchers are the same as those used in my reducers, correct? For example, if I h ...