1

How can i update variable from function? I have this. But both variables still "0" after "updateFilter()". How can i do this without run "loadAjax(var1, var2)" from slider function?

var var1 = 0,
    var2 = 0;

$("#slider").slider({
    // options...
    stop: function( event, ui ) {
        var var1 = ui.values[ 0 ], // <- changes on move
            var2 = ui.values[ 1 ]; // <- changes on move
        updateFilter();
    }
});

function updateFilter() {
    loadAjax(var1, var2);
}

sorry for my english

2 Answers 2

1

Problem with you code is you are defining same varible you defined in global scope

var var1 = 0,
    var2 = 0;

//in function below you are redefining var1 and var2 varible please dont do that 
 stop: function( event, ui ) {
        var var1 = ui.values[ 0 ], // <- changes on move
            var2 = ui.values[ 1 ]; // <- changes on move
        updateFilter();

instead of above code stop method should be

 stop: function( event, ui ) {
            var1 = ui.values[ 0 ], // <- changes on move
            var2 = ui.values[ 1 ]; // <- changes on move
        updateFilter();

why dont you pass varible to function directly like this

updateFilter(var1, var2);


function updateFilter(var1, var2) {
    loadAjax(var1, var2);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thanks! It works (im new in JS and didn't know these details)
0

http://jsfiddle.net/kcg1hmsu/

your values are 0, because you're shadowing your global variables, you can simply remove the var inside the stop function

$("#slider").slider({
    // options...
    stop: function( event, ui ) {
         var1 = ui.values[ 0 ], // <- changes on move
         var2 = ui.values[ 1 ]; // <- changes on move
         console.log(var1, var2);
        updateFilter();
    }
});

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.