Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:35:34+00:00 2026-05-26T23:35:34+00:00

I have 5 spinners which are almost working perfectly, there is just one little

  • 0

I have 5 spinners which are almost working perfectly, there is just one little glitch I cannot overcome.

What is suppose to happen is that if a spinner is blank, whatever the previous number was in THAT spinner, it will display that previous number.

Instead what it is doing is that any previous number that has been entered in ANY of the 5 spinners, it displays that number in the blank spinner.

So how can I get it so that it displays the previous number from that spinner if a spinner is blank rather than displaying the last previous number entered from any spinner in the blank spinner?

Below is my spinner code:

     function Spinner(elem,min, max){
 this.elem = elem;
 this.elem.value = min;
 this.min = min;
 this.max = max;
 this.timer;
 this.speed = 150; //milliseconds between scroll values
 var selfO = this;

 this.elem.onkeyup = function(){
    var regex = /^[0-9]*$/;
    if(!regex.test(selfO.elem.value)){
    selfO.elem.value = selfO.elem.value.substring(0,selfO.elem.value.length-1);
                            return;
                        }
                        selfO.validateValue();
                    }

      this.validateValue = function(){
      if(Number(selfO.elem.value) > selfO.max) {selfO.elem.value = selfO.max;}
      if(Number(selfO.elem.value) < selfO.min) {selfO.elem.value = selfO.min;}
                    }

                    this.stopSpinning = function(){
                        clearTimeout(selfO.timer);
                    }

                    this.spinValue = function(dir){
                        selfO.elem.value = Number(selfO.elem.value) + dir;
                        selfO.validateValue();
                        selfO.timer = setTimeout(function(){selfO.spinValue(dir);},selfO.speed);
                    }

                };

           window.onload=function(){
                        //create the Spinner objects
                        var SpinnerHours = new Spinner(document.getElementById('txtHours'),0,23);                
                        var SpinnerMins = new Spinner(document.getElementById('txtMins'),0,59);
                        var SpinnerSecs = new Spinner(document.getElementById('txtSecs'),0,59);
                        var SpinnerWeight = new Spinner(document.getElementById('txtWeight'),0,100);
                        var SpinnerQuestion = new Spinner(document.getElementById('txtQuestion'),0,100);

                        document.getElementById('txtHours').onblur = function(){ this.value = cleanSpin(this.value);}
                        document.getElementById('txtMins').onblur = function(){ this.value = cleanSpin(this.value);}
                        document.getElementById('txtSecs').onblur = function(){ this.value = cleanSpin(this.value);}
                        document.getElementById('txtWeight').onblur = function(){ this.value = cleanSpin(this.value);}
                        document.getElementById('txtQuestion').onblur = function(){ this.value = cleanSpin(this.value);}

            function cleanSpin(obj) {
                if(obj > 0) {
                    var str = obj.replace(/^0*/,'');
                    lastSpin = str;
                    return str;
                }
                else {
                    return lastSpin;
                }
            }

            var lastSpin = 1;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:35:35+00:00Added an answer on May 26, 2026 at 11:35 pm

    The problem is that your lastSpin variable, and indeed, your cleanSpin() function, are in the global scope, and thus shared across all Spinner instances.

    Instead, you should try adding the cleanSpin method, or a variant thereof, to the Spinner “class”, so that it becomes local to that instance of the Spinner.

    EDIT: Here is the complete Javascript to illustrate what I meant. I have not tested this at all, as I do not have the rest of the code needed to do so, but hopefully you can see what I have changed:

    function Spinner(elem, min, max) {
        this.elem = elem;
        this.elem.value = min;
        this.min = min;
        this.max = max;
        this.timer;
        this.speed = 150; //milliseconds between scroll values
        this.lastSpin = 1; // added lastSpin as a property of the Spinner
        var selfO = this;
    
        this.elem.onkeyup = function() {
            var regex = /^[0-9]*$/;
            if (!regex.test(selfO.elem.value)) {
                selfO.elem.value = selfO.elem.value.substring(0, selfO.elem.value.length - 1);
                return;
            }
            selfO.validateValue();
        }
    
        // I have added the onblur function inside the Spinner "class"
        this.elem.onblur = function() {
            if (self0.elem.value > 0) {
                var str = self0.elem.value.replace(/^0*/, '');
                self0.lastSpin = str;
                self0.elem.value = str;
            }
            else {
                self0.elem.value = lastSpin;
            }
        };
    
        this.validateValue = function() {
            if (Number(selfO.elem.value) > selfO.max) {
                selfO.elem.value = selfO.max;
            }
            if (Number(selfO.elem.value) < selfO.min) {
                selfO.elem.value = selfO.min;
            }
        };
    
        this.stopSpinning = function() {
            clearTimeout(selfO.timer);
        };
    
        this.spinValue = function(dir) {
            selfO.elem.value = Number(selfO.elem.value) + dir;
            selfO.validateValue();
            selfO.timer = setTimeout(function() {
                selfO.spinValue(dir);
            }, selfO.speed);
        };
    
    };
    
    window.onload = function() {
        //create the Spinner objects
        var SpinnerHours = new Spinner(document.getElementById('txtHours'), 0, 23);
        var SpinnerMins = new Spinner(document.getElementById('txtMins'), 0, 59);
        var SpinnerSecs = new Spinner(document.getElementById('txtSecs'), 0, 59);
        var SpinnerWeight = new Spinner(document.getElementById('txtWeight'), 0, 100);
        var SpinnerQuestion = new Spinner(document.getElementById('txtQuestion'), 0, 100);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one spinner in which few values are there from strings.xml and I
I'm trying to implement multiple spinners within one activity, which appears to be working
Ok so I am working on an android app and I have implanted spinners...
I have a spinner, which mostly works. If a user selects one of the
I have a simple spinner which has several number options. I want to just
I am developing an android app in which I have 3 spinners populated from
I have developed part of an application which contains a number of spinners to
I have three web Views next to one another, for display of seperate spinners.
I have 3 spinners in my xml.. 1. one is Product Name 2. one
I have a Main-Activity which displays several spinners. With a Toggle-Button in the Main-Activity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.