0

Here is the scenario that I have to solve,

  1. There are two views in the page.
  2. The first view is loaded initially.
  3. The data for second view is loaded only when the user clicks on the second view.

Problem:

  1. The user clicks on the second view.
  2. The ajax request takes time to respond.
  3. The user so goes back to the first view.
  4. Now the ajax request's callback is called.
  5. The user is currently in first view but the content is of the second view's.

How do I handle this scenario?

Are there any generic ways to fix such problems or should it be fixed on a case by case scenario.

Gautham Renganathan
  • 647
  • 1
  • 9
  • 18
  • You can have a loding gif image to show that the ajax content is loading. – Tushar Dave May 30 '14 at 11:58
  • use a loading indicator? – charlietfl May 30 '14 at 11:58
  • What if the user still goes ahead and clicks? Would'nt it be a problem then? – Gautham Renganathan May 30 '14 at 12:01
  • @GauthamRenganathan you can stop the user for going to first view while the gif image is showing by disabling the click on screen anywhere while the image is loading.If you use a fancybox to show the loading gif then it can be done by setting modal property of fancybox to true as modal:true. – Tushar Dave May 30 '14 at 12:03
  • @TusharDave : Is there any other way to handle this. Because I do not want to hinder the users experience. Can I silently ignore the request's callback now that the user has switched his view? – Gautham Renganathan May 30 '14 at 12:06
  • @GauthamRenganathan yes u can do that ,Just keep a global variable in js that serves as a flag to let u know that on which view the user currently is then using that you can silently ignore the request's callback. – Tushar Dave May 30 '14 at 12:13
  • Ok. So this has to be done individually in all scenarios wherever such problem occurs in the application. Right? – Gautham Renganathan May 30 '14 at 12:17
  • @GauthamRenganathan if you have less no of scenarios in the application then its better to have this but if you think that there could be a good no of such scenarios i would recommend to use the loading gif as it would be very clumsy to use flags for each instance and could create confusion. – Tushar Dave May 30 '14 at 12:21
  • I think your problem have two sides. 1) Your app must be user friends, so show to use some message 'Load of ... need some time.. bla bla bla'. 2) If user moves from second view to the first, just abort your query. thats all. – Farkhat Mikhalko May 30 '14 at 12:27

3 Answers3

0

So if understand you right, you have a process, which you want to see on both views?

You could make a Call that starts the process. Set somewehere via Filesystem/Session/Database the current state (You want to make sure, you have no locking for read access and have timestamps to make sure you delete dead data, if a fatal error orcurs)

Your States could be Integer:

  • 1: Process started
  • 2: First Method Handling
  • 3: Third Method Handling
  • 4: Finished

You can also write for each Step an Output File/Record/Session

On the 2 views you ask every 5000 MS the Server: Do you have something for me? If so, you will display it.

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
0
  1. Add a loading indicator to the second view, so that the user know something is happening and he/she is supposed to wait.

  2. When going back to first view, from second view, you can always abort the AJAX request. More information here:

Abort Ajax requests using jQuery

Community
  • 1
  • 1
Shishir
  • 2,488
  • 20
  • 22
0

jQuery ajax methods return XMLHttpRequest object, so you can just use .abort()

For suppose, this is your ajax call for second view :

view2_ajax = $.ajax({
    url: 'your_view2_url'
});
view2_ajax.done(function(result){
    // do something for success
});
view2_ajax.fail(function(result){
    // do something for fail
});

So when user clicks on first view just call view2_ajax.abort() and that ajax call will be canceled.

Visit jqXHR for more information (jQuery API documentation).

Jalay
  • 66
  • 1
  • 7