1

In jQuery Mobile, if user clicks on a button, a loading icon appears and then it loads the new webpage via Ajax.

But, The server may be not respond in my case. Isn't there any way to put a timeout (e.g. 10 seconds) for ajax navigation feature? (If time limit exceeds, stop trying to navigate and show an error message)

Mahdi Ghiasi
  • 14,873
  • 19
  • 71
  • 119
  • already asked http://stackoverflow.com/questions/5225597/set-timeout-for-ajax-jquery – zizoujab Aug 05 '12 at 13:53
  • I don't understand their answer properly, Where should I put that code? And How to show error message? Can you give an example? – Mahdi Ghiasi Aug 05 '12 at 14:01
  • if you have written some code post it so we can see what is wrong with it. because in jQuery and jQuery mobile there is many ways to make an ajax call. – zizoujab Aug 05 '12 at 14:13
  • I have a jQuery mobile page with a link, like this: `Click here`. But I don't know actualy what should I do to set timeout for this ajax call. I prefer a global way for all of links, if possible. – Mahdi Ghiasi Aug 05 '12 at 14:58

1 Answers1

1

To set a timeout i think you should not do it in a static way ( using "data-transition" ) you can make a listener to the link ('onclick') and within the listener make an ajax call to load your page. Use $.mobile.changePage() to do that.

The $.mobile.changePage() function is used in a number of places in jQuery Mobile. For example, when a link is clicked, its href attribute is normalized and then $.mobile.changePage() handles the rest.

so your code could seem like this :

$('#link_id').click(function() {
    $.ajax({
    url: "page_served_from_server",
        error: function(jqXHR, strError){
        if(strError == 'timeout')
        {
          //do something. Try again perhaps?
        }
    },
    success: function(){
        //charge your page :
       // $.mobile.changePage('yourPageAdress',"turn",false,true);
    },
    // here you can specify your timeout in milliseconds 
    timeout:3000
    });
});
Azteca
  • 549
  • 6
  • 19
zizoujab
  • 7,603
  • 8
  • 41
  • 72