-1

I have two functions country and location. When the page loads these two functions will run.

I need to get the result value from $.getScript and use it in the function location().

Currently, I am getting undefined. Please check my code.

Any help would be appreciated.

<script>
    $(function () {
        var result;

        function country() {
            $.getScript("ajax/country.js", function () {
                result = 'data one'; // I need to get this result in location().
            });
        }

        function location() {
            console.log(result); // This result should be 'data one'. Currently, I am getting undefined.
        }

        country();

        location();

    });
</script>
Sarath TS
  • 2,432
  • 6
  • 32
  • 79

1 Answers1

0

$.getScript() works asynchronous. Call location in the getScript function.

$(function () {
    function country() {
        $.getScript("ajax/country.js", function () {
            let result = 'data one'; // I need to get this result in location().
            location(result);
        });
    }

    function location(result) {
        console.log(result); // This result should be 'data one'. Currently, I am getting undefined.
    }

    country();
});
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29