Return desired value within a function

2

When trying to return the value of $("#datepicker-range").val() at the beginning of the code, console gives me an empty value. So I was suggested to do a page sniffer because the reason the field was not capturing the date field would be due to a problem in the order of operations on reading the code.

$(function(){
    startSelectors();

    var datepickerVerify = setInterval(function(){
        if($("#datepicker-range").val()){
            clearInterval(datepickerVerify);
            var vl = $("#datepicker-range").val();
            console.log('value returned: '+vl);
        }
        return vl;
    }, 50);

    //verificando o valor da variavel
    console.log('datepicker=> '+datepickerVerify);
});


function startSelectors(){
    $("#datepicker-range").val('2017-03-01 até 2017-08-02');
};

Instead of returning the date in the middle log, as it was returned inside if , it returns me a true (number 1). How can I set the value of the variable datepickerVerify instead of the true , to return the date?

    
asked by anonymous 14.08.2017 / 16:39

2 answers

0

Hello,

By separating the contents of the setInterval function into a separate routine, the code worked perfectly. I commented on the clearInterval routine because her code was not in the example. Here is the code:

 $(document).ready(function(){
        startSelectors();

        var datepickerVerify = setInterval();

        //verificando o valor da variavel
        console.log('datepicker=> '+datepickerVerify);
    });

function setInterval(){
            if($("#datepicker-range").val()){
                //clearInterval(datepickerVerify);
                var vl = $("#datepicker-range").val();
                console.log('value returned: '+vl);
            }
            return vl;
};

function startSelectors(){
    $("#datepicker-range").val('2017-03-01 até 2017-08-02');
};
    
15.08.2017 / 02:06
0

In this case, this behavior is due to the fact that you are using setInterval, which by its characteristic starts processing in parallel with the execution of the main code.

The behavior of the interpreter was this: 1. Called and executed the startSelectors 2. Created a new instance to execute the function passed in setInterval after 50 milliseconds 3. You printed the console.log

But when he performs step 2, he does not wait for the 50 millisecond time count to proceed with the execution, he goes straight to the end and in parallel runs the setInterval after the set time.

This is visible even in the console, which first prints the console.log from outside the setInterval and then prints the console.log from inside the setInterval, because it was executed just after.

To solve this, check if you really need this setInterval, by removing it you get back to a linework line. If it is really necessary, you will have to proceed with the logic inside the setInterval or with a function call in callback, as in the example below:

$(function(){
    startSelectors();

    setInterval(function(){
        if($("#datepicker-range").val()){
            clearInterval(datepickerVerify);
            var vl = $("#datepicker-range").val();
            console.log('value returned: '+vl);
        }
        continuacao(vl);
        //return vl;
    }, 50);

    //verificando o valor da variavel
    function continuacao(datepickerVerify) {
      console.log('datepicker=> '+datepickerVerify);
    }
});


function startSelectors(){
    $("#datepicker-range").val('2017-03-01 até 2017-08-02');
};
    
15.08.2017 / 02:18