0

I have Following code, I am checking height and width of image. this is a part of script. Please help me for this. Thanks in advance

_filesCheck: function() {    
                var result = false;

                    if(n.ftype=='image')
                    {
                            var reader = new FileReader();
                            //Read the contents of Image File.
                            reader.readAsDataURL(f.files[0]);
                            reader.onload = function (e) {
                                //Initiate the JavaScript Image object.
                                var image = new Image();

                                //Set the Base64 string return from FileReader as source.
                                image.src = e.target.result;

                                //Validate the File Height and Width.
                                image.onload = function () {
                                    var height = this.height;
                                    var width = this.width;
                                    if (height != n.hsize && width != n.wsize) {
                                        alert("Width and Height must "+n.wsize+"x"+n.hsize+"");
                                        result = false;
                                        return false;
                                    }
                                    /*alert("Uploaded image has valid Height and Width.");*/
                                    result = true;
                                    return true;
                                };

                            }

                            alert(result);                          

                        return result; }}

I want to get true or false in result. But it always give me false. and it get alert before the condition in function

rp07
  • 1
  • 2
  • The functions `onload` of `reader` and `image` are called after the asynchronous taks is completed. Please see the answers on the linked question to get a way to return response. – Tushar Sep 01 '16 at 12:24
  • An event handler is a function which will be executed *later* when the event actually happens. This code doesn't all execute top-to-bottom in the order it's written. – David Sep 01 '16 at 12:25
  • @Tushar I have seen this link. But nothing found helpful. Can you please provide me code for my script. I am stucked. Please help – rp07 Sep 01 '16 at 12:30
  • Instead of returning the response, call a function and pass the data as param to that function and do further processing in that function. You can read more on this in linked post under heading **Let functions accept callbacks** – Tushar Sep 01 '16 at 12:32
  • after calling a function. How it will return true or false to main function? – rp07 Sep 01 '16 at 12:34
  • It won't. When it returns to the main function, there is no result yet. – Bergi Sep 01 '16 at 12:35
  • @bergi Please tell me any solution for this – rp07 Sep 01 '16 at 12:37
  • @rp07 See the duplicate. You will have to restructure your main function. – Bergi Sep 01 '16 at 13:04

0 Answers0