EasyUI Forum
April 28, 2024, 01:59:33 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: $.messager.progress and ajax  (Read 7138 times)
Juan Antonio Martínez
Jr. Member
**
Posts: 68



View Profile
« on: October 25, 2018, 05:40:43 AM »

Hello.
I think this is a classic problem, but don't know the right way to solve

Need to show a progressbar and hide it when an ajax call gets done (either success or fail).
In some scenarios ajax call finishes _before_ progressbar is shown, thus the progress bar never closes.
Whats the right way to write this code?

Code:
function doSomeThingWithAjax() {
    $.messager.progress({height:75, text:'Working'});
    $.ajax({
        type: 'GET',
        url: myUrl,
        data: { ...        },
        dataType: 'json',
        success: function (result) { .... },
        error: function() {....},
        complete: function() {
              $.messager.progress('close');
        }
    });
}

Thanks in advance
Juan Antonio
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: October 25, 2018, 05:12:23 PM »

Display the message dialog in 'beforeSend' and close it in 'complete'. Please refer to the code below:
Code:
$.ajax({
url: 'get_users.php',
method: 'post',
beforeSend:function(){
$.messager.progress({height:75, text:'Working'});
},
complete: function(){
$.messager.progress('close');
}
})
Logged
Juan Antonio Martínez
Jr. Member
**
Posts: 68



View Profile
« Reply #2 on: October 26, 2018, 01:09:01 AM »

Mmmmh doesn't work for me :-(. The problem is that $.messager.progress fires up an async task (timer) to handle its internals

Also, I use a global beforeSend options for every ajax calls to insert special headers, so no easy way of insert specific messages into each progress bar and/or omit them on ajax calls that doesn't need progressbar

This seems to work fine:
Code:
function doSomeThingWithAjax() {
    $.when( function() {
        $.messager.progress({height:75, text:'Working'});
    })
    .then( function() {
        $.ajax({
            type: 'GET',
            url: myUrl,
            data: { ...        },
            dataType: 'json',
            success: function (result) { .... },
            error: function() {....},
       });
    })
    .then( function() {
              $.messager.progress('close');
   });
}
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!