How to force .trigger (...)?

I have a button that performs heavy calculations, and between the steps of the calculation I want to refresh the page.

JavaScript:

.on('click', function() { // compute something jQuery('#page') .trigger('pageshow') ; // compute something jQuery('#page') .trigger('pageshow') ; // compute something } 

This, unfortunately, does not work, because pageshow -event is launched after the completion of .on(click) .

Is it possible to force execution of .trigger(...) ?

JavaScript is an ugly solution:

 .on('click', function() { // compute something window.setTimeout(function() { // compute something jQuery('#page') .trigger('pageshow') ; window.setTimeout(function() { // compute something jQuery('#page') .trigger('pageshow') ; }, 1); }, 1); } 

An ugly solution will work, but actually a pretty ugly one. It is also necessary to check whether the .on('click', ...) -event is fully computed at startup.

JavaScript is a prettier but still ugly solution:

 var functionFirst = function() { // compute something jQuery('#page') .trigger('pageshow') ; window.setTimeout(functionSecond, 1); }; var functionSecond = function() { // compute something jQuery('#page') .trigger('pageshow') ; window.setTimeout(functionThird, 1); }; var functionThird = function() { // compute something jQuery('#idJqGame') .trigger('pageshow') ; }; window.setTimeout(functionFirst, 1); 

A more beautiful, but still ugly solution basically does the same thing as an ugly one - it’s just easier to read, and therefore I don’t know it yet.

+4
source share
2 answers

The JavaScript interpreter is built around an event loop. Thanks to this design, and since the interpreter runs in a single thread, all events are processed sequentially.

If this is possible in the design of your application, try making a regular page refresh call instead of an event.

Another option, if you can guarantee that your application will only run in modern browsers, is to move the “heavy computing” to the web worker. Further information here: https://developer.mozilla.org/en/Using_web_workers

EDIT 1:

According to the Attached Events section of http://dev.opera.com/articles/view/timing-and-synchronization-in-javascript/ , you may be able to achieve what you want if you use the native browser function for queues of your events

Another elegant solution might be to use sequential event processing. Move heavy computing to the event handler. Then press "compute" and "pageshow" one at a time so that they are alternatively called. This suggests that I'm afraid it will make the page look unresponsive, as regular DOM events will also be queued after.

EDIT 2:

Just found this: http://api.jquery.com/queue/ Help.

+3
source

I'm sorry to say that an ugly solution is the best.
It may be exaggerated, but the point is that you need to break your code into smaller blocks in order to be able to control them.

+2
source

All Articles