0

I'm trying to return a value from inside jQuery.post inside a function, so far the value returned is undefined.

How do i solve?

function check_file(title) {
                    var url = "http://www.example.com/wp-content/check_download.php";

                    var data = {
                        title:title
                    };


                    jQuery.post(url,data,function(response) {


                        if(response==1) {
                            //if file exists
                            return 1;
                        } else {
                            //file doesn't exist keep checking until it does
                            setTimeout(function () {
                                check_file(title);
                            }, 1000);
                        }

                    });         
                }

It should return 1 from the function check_file but it returns undefined how do i solve?

mdnba50
  • 369
  • 4
  • 23
  • your return is in the function in the post, then `return 1` is the return of this function, not the check_file function. You'll have to reconsidere what you'are doing here. – Mohicane Apr 13 '17 at 15:11
  • It is not useful to return values from these callbacks. Instead, they need to have a side-effect to communicate their results (set a global variable or change the DOM or something). Maybe take a look at the `Promise` pattern. Then other code can "wait" for future results. – Thilo Apr 13 '17 at 15:20

0 Answers0