user892134
user892134

Reputation: 3214

setInterval not working jquery

When i click and hold down the value should continually decrease until i mouseup or mouseleave. Instead i have to keep on clicking down. Here is is the jsfiddle http://jsfiddle.net/mdanz/5mdwe/8/

$('#bid-down').live('mousedown',function() {

var button = $(this);
var number = $(this).parent('div').siblings('#bid-child-container-2').find('#bid-price').val();
var newnumber = number.split('.');


button.data('interval2', setInterval(function (){

var on = button.attr('data-on');

if(newnumber[0]==2 && newnumber[1]==5) {
    alert('Minimum Bid($2.50)');
    }

if(on==1) {
    if(newnumber[0]>2) {
        if(newnumber[1]==0) {
        var first = parseInt(newnumber[0])-1;    
        var second = 9;    
        }
    }

    if(newnumber[1]==0) {
    var first = parseInt(newnumber[0])-1;    
    var second = 9;
    }

    if(newnumber[1]>0) {
    var first = newnumber[0];    
    var second = parseInt(newnumber[1])-1;    
    }

    if(first==2 && second==5) {
    button.attr('data-on','0');
    }

    var finalnumber = first+'.'+second;
    button.parent('div').siblings('#bid-child-container-2').find('#bid-price').val(finalnumber);

}

}, 100));


}).bind('mouseup mouseleave', function() {

clearInterval($(this).data('interval2'));
});

Upvotes: -1

Views: 373

Answers (2)

bart s
bart s

Reputation: 5110

You interval is working perfectly. However you do not handle number and newnumber inside the setInterval functionality

if you add something like console.log('down'); and then look in the error console of the browser, you can see that your setInterval is executing repeatedly

With your existing code you need to change a few lines see http://jsfiddle.net/5mdwe/23/

Upvotes: 1

Paul
Paul

Reputation: 9022

You need to put the code lines to extract the number INSIDE the interval function

-OR-

set the value of number/newnumber at the end of the interval function.

See demo.

Upvotes: 1

Related Questions