Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript use return value in another function

What I want to achive is using the return value of the "previewfile" function as an execution indicator for the "readfiles" function. But this needs to be after the "image.onload" part has been executed, since there I need returnThis to be set to true. I've researched several things on Google and Stackoverflow concerning this problem and callbacks / deferred objects in general, but I cannot wrap my head around how to applicate that in this situation.

I have the following constellation in my Image uploading section:

function previewfile(file, tests, acceptedTypes, holder) {
    var returnThis = false;                                                                               
    if (tests.filereader === true && acceptedTypes[file.type] === true) {                                                                
        var reader = new FileReader();                                                                                                   
        reader.onload = function (event) {                                                                                               
            var image = new Image();                                                                                
            image.onload = function() {                                                                                                  
                var testimage = new Image();                                                                                             
                testimage.src = $(this).attr('src');                                                                                     
                var widthOfImage = testimage.width;                                                                                      
                var heightOfImage = testimage.height;                                                                                    
                if (!checkImageDimensions(widthOfImage, heightOfImage)) {
                    // do stuff                                                  
                } else {
                    returnThis = true;                                                                                           
                }                                                                                                                        
            };                                                                                                                           
            image.src = event.target.result;                                                                                             
            holder.appendChild(image);                                                                                                   
        };                                                                                                                               
        reader.readAsDataURL(file);                                                                                                      
    }  else {                                                                                                                            
        // do other stuff                                                                                                              
    }                                                                                                                                    
    return returnThis;                                                                                                                   
}                                                                                                                                        
function readfiles(files, tests, acceptedTypes, holder, progress) {                                                                      
    var uploadNow = previewfile(files[0], tests, acceptedTypes, holder);                                                                   
    if (uploadNow === true) {                                                                                                               
        // do stuff                                                                                                          
        }                                                                                                                                
    } else {                                                                                                                             
        // do other stuff                                                                                                    
    }                                                                                                                                    
}                                                                                                       
like image 375
plocks Avatar asked Oct 01 '22 01:10

plocks


1 Answers

I would go with something like this

function readfiles(files, tests, acceptedTypes, holder, progress) {                                                                      
    previewfile(files[0], tests, acceptedTypes, holder, function(value){
        if (uploadNow === true){                                                                                                               
             // do stuff                                                                                                          
        }                                                                                                                                
        else {                                                                                                                             
            // do other stuff                                                                                                    
        } 
    });                                                                   

}

function previewfile(file, tests, acceptedTypes, holder, callback) {
    ...
    callback(returnValue); //instead of return
}
like image 172
kallehj Avatar answered Oct 15 '22 10:10

kallehj