简体   繁体   中英

force safari to repaint DOM

Apparently, Safari does not repaint the DOM during the submit event. I have code that contains an ajax function inside the submit handler. I cannot change the action of the form or the submit button to <button> . Inside the ajax function I am calculating the width of a progress bar and adjusting a div accordingly. This works in Chrome and FF. Not in Safari. When I stop the browser, the progress bar shows.

I want to know how to force Safari to repaint the DOM inside my ajax function. It looks like this:

$('#webfileuploadform').bind('submit',function(){
    var $progress = $('<div id="upload-progress" class="upload-progress"></div>').appendTo($('#webfileuploadform')).append('<div class="progress-container"><div class="progress-info">uploading 0%</div><div class="progress-bar"></div></div>');
    function update_progress_bar(){
        $.ajax({
            dataType:"json",
            async: false,
            url: progress_url,
            success: function(data){


                $progress.show();  // This shows the progress bar. I imagine 
                                   // this is where I would want to re-paint the DOM


                if(data.status == "uploading"){
                    var total_progress = parseInt(data.uploaded) / parseInt(data.length);
                    var width = $progress.find('.progress-container').width();
                    var progress_width = Math.round(width * total_progress);
                    var progress_px = progress_width + 'px';
                    $progress.find('.progress-bar').animate({'width':progress_px});
                    $progress.find('.progress-info').text('uploading ' + parseInt(total_progress*100) + '%');
                } else if (data.status == "not-found") {
                    $progress.find('.progress-bar').animate({'width':'100%'});
                    $progress.find('.progress-info').text('uploading 100%');
                    stop = 'Flag for end-of-file';
                } else {
                    //console.log('Ajax Error');
                }
            },
            error: function(textStatus){
                console.log("ERROR: " + textStatus);
            }
        });

        // Check flag to see if user stopped the page to cancel the ajax requests
        if(typeof stop == 'undefined'){
            // Iterates the ajax requests
            setTimeout(function(){update_progress_bar()}, freq);
        } else {
            setTimeout(function(){
                $progress.animate({opacity:0.5},1000);
            },freq);
            $progress.find('.progress-info').text('Refresh the page to continue.');
        }
    }

    // Initiate the ajax requests
    setTimeout(function(){update_progress_bar()}, freq);

});

I would recommend using forceDOMReflowJS . In your case, your code would look as follows. Also, your code doesn't exactly work as I believe it is intended so I fixed your code up too.

$('#webfileuploadform').bind('submit',function(){
    var $progress = $('<div id="upload-progress" class="upload-progress"></div>');
    // I believe you forgot to separate your attachments section of the code from
    // the variable declaration
    $progress.appendTo($('#webfileuploadform')).append('<div class="progress-container"><div class="progress-info">uploading 0%</div><div class="progress-bar"></div></div>');
    function update_progress_bar(){
        $.ajax({
            dataType:"json",
            async: false,
            url: progress_url,
            success: function(data){
                $progress.show();
////////////////////////////////////////////////////////////////////////////////
                // Perform the reflow (even though I don't really see how it would change things)
                forceReflowJS( $progress[0] );
////////////////////////////////////////////////////////////////////////////////
                if(data.status == "uploading"){
                    var total_progress = parseInt(data.uploaded) / parseInt(data.length);
                    var width = $progress.find('.progress-container').width();
                    var progress_width = Math.round(width * total_progress);
                    var progress_px = progress_width + 'px';
                    $progress.find('.progress-bar').animate({'width':progress_px});
                    $progress.find('.progress-info').text('uploading ' + parseInt(total_progress*100) + '%');
                } else if (data.status == "not-found") {
                    $progress.find('.progress-bar').animate({'width':'100%'});
                    $progress.find('.progress-info').text('uploading 100%');
                    stop = 'Flag for end-of-file';
                } else {
                    //console.log('Ajax Error');
                }
            },
            error: function(textStatus){
                console.log("ERROR: " + textStatus);
            }
        });

        // Check flag to see if user stopped the page to cancel the ajax requests
        if(typeof stop == 'undefined'){
            // Iterates the ajax requests
            setTimeout(function(){update_progress_bar()}, freq);
        } else {
            setTimeout(function(){
                $progress.animate({opacity:0.5},1000);
            },freq);
            $progress.find('.progress-info').text('Refresh the page to continue.');
        }
    }

    // Initiate the ajax requests
    setTimeout(function(){update_progress_bar()}, freq);

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM