I have a webpage, in which a certain Ajax event is triggered asynchronously. This Ajax section could be called once or more than once. I do not have control over the number of times this event is triggered, nor the timing.
Also, there is a certain code in that Ajax section that should run as a critical section, meaning, when it is running, no other copy of that code should be running.
Here is a pseudo code:
- Run JavaScript or jQuery code
- Enter critical section that is Ajax (when a certain process is waiting for a response callback, then do not enter this section again, until this process is done)
- Run more JavaScript or jQuery code
My question is, how can I run step 2 the way described above? How do I create/guarantee a mutual exclusion section using JavaScript or jQuery.
I understand the theory (semaphores, locks, ...etc.), but I could not implement a solution using either JavaScript or jQuery.
EDIT
In case you are suggesting a Boolean variable to get into the critical section, this would not work, and the lines below will explain why.
the code for a critical section would be as follows (using the Boolean variable suggestions):
load_data_from_database = function () {
// Load data from the database. Only load data if we almost reach the end of the page
if ( jQuery(window).scrollTop() >= jQuery(document).height() - jQuery(window).height() - 300) {
// Enter critical section
if (window.lock == false) {
// Lock the critical section
window.lock = true;
// Make Ajax call
jQuery.ajax({
type: 'post',
dataType: 'json',
url: path/to/script.php,
data: {
action: 'action_load_posts'
},
success: function (response) {
// First do some stuff when we get a response
// Then we unlock the critical section
window.lock = false;
}
});
// End of critical section
}
}
};
// The jQuery ready function (start code here)
jQuery(document).ready(function() {
var window.lock = false; // This is a global lock variable
jQuery(window).on('scroll', load_data_from_database);
});
Now this is the code for the lock section as suggested using a Boolean variable. This would not work as suggested below:
The user scrolls down, (and based on the association
jQuery(window).on('scroll', load_data_from_database);
more than one scroll event is triggered.Assume two scroll events are triggered right at almost the same moment
Both call the
load_data_from_database
functionThe first event checks if
window.lock
is false (answer is true, so if statement is correct)The second event checks if
window.lock
is false (answer is true, so if statement is correct)The first event enters the if statement
The second event enters the if statement
The first statement sets
window.lock
to trueThe second statement sets
window.lock
to trueThe first statement runs the Ajax critical section
The second statement runs the Ajax critical section.
Both finish the code
As you notice, both events are triggered almost at the same time, and both enter the critical section. So a lock is not possible.
true
when the critical section is called. When it's done, set it tofalse
. Of course, at the beginning of the code, make sure it's not already set totrue
– Sillabubwindow.lock
variable "at the same time" (or almost the same time). If you need all executions of the AJAX triggered by the scroll to fire, and the 2nd, 3rd, etc. to wait for the 1st to finish, that is a mildly different question (one for which I'm writing an answer). – Conducive