0

The first time around increment is 10, like it should be, the second iteration returns undefined. I assume that it has got something to do with setTimeout?

var time = 200;

Function:

function repeatingfunction(increment) { 
    if (time>5){
        time=time-39
    };
    if (mausunten == true){
        //alert(increment);
        setTimeout(repeatingfunction, time);
        $("#slider-vertical").slider("value", ($("#slider-vertical").slider("value"))+increment); 
        refreshCalculation();
    };
    if (mausunten == false){
        clearTimeout(repeatingfunction);
    };
}

Calling and stopping the function:

$("#plus1").mousedown(function(){
    mausunten = true;
    repeatingfunction(10);
});

$(document).mouseup(function() {
    mausunten = false;
    time = 200;
});
2
  • 2
    If you want quick answers to something like this there are three things you need to do: 1. Create a JSFiddle 2. Create a JSFiddle and lastly 3. Create a JSFiddle :) Commented Sep 17, 2013 at 11:32
  • 1
    is mausunten declared globally Commented Sep 17, 2013 at 11:32

1 Answer 1

2

You're not passing the value in to subsequent calls. Try this...

var timeoutID;

function repeatingfunction(increment) {
    if (time>5){
        time=time-39
    };
    if (mausunten == true){
        //alert(increment);
        timeoutID = setTimeout(function() {
            repeatingfunction(increment);
        }, time);
        $("#slider-vertical").slider("value", ($("#slider-vertical").slider("value"))+increment); 
        refreshCalculation();
    };
    if (mausunten == false){
        clearTimeout(timeoutID);
    };
}

I added a function into setTimeout so that you can pass the value in.

Also, I fixed the clearTimeout. You were passing the function name into it and it expects an ID that refers to the timer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.