Javascript wait for image to load before calling Ajax

前端 未结 2 2027
粉色の甜心
粉色の甜心 2021-01-26 03:18
function dropResource() {

    var imgIndex = getImageIndexByID(currentDragImageID);
    var newImgID = resourceData.length;

    // Create the image
    $(\'#thePage\')         


        
2条回答
  •  旧巷少年郎
    2021-01-26 03:41

    When you set the src property the browser starts to download the image immediately. You have to define a load event prior to that at insert you post-load code inside it:

    $('Big').load(function() {
    
        // you can do things here, after the image loads
        var imgW = $(this).width();
        var imgH = $(this).height();
        // ...
    
    }).appendTo('#thePage').attr('src',uploadFolder + '/' + imgData[imgIndex][1]);
    

提交回复
热议问题