返回父函数的值,如何等待异步函数

时间:2016-09-01 12:22:45

标签: javascript jquery function asynchronous

我有以下代码,我正在检查图像的高度和宽度。 这是脚本的一部分。请帮帮我。提前致谢

_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; }}

我想在结果中得到真或假。但它总是让我虚伪。它在功能

之前得到警报

0 个答案:

没有答案