Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  39] [ 5]  / answers: 1 / hits: 17804  / 15 Years ago, sat, may 9, 2009, 12:00:00

I was wondering whenever exists a solution to perform synchronization in JavaScript code. For example I have the following case: I'm trying to cache some response values from AJAX call, the problem is, that it's possible to perform simultaneously several calls, therefore it leads to race condition in the code. So I'm very curious to find solution for that? Any one has idea that to do?


More From » ajax

 Answers
226

I have found solution for my problem. I have to say it's not that perfect as I was looking for, but so far it works and I think about that more as temporarily work around.



$.post( url1, function( data)
{
// do some computation on data and then
setSomeValue( data);
});

var setSomeValue = ( function()
{
var cache = {};
return function( data)
{
if ( cache[data] == updating)
{
setTimeout( function(){ setSomeValue( data);}, 100);
return;
}
if ( !cache[date])
{
cache[date] = updating;
$.post( url2, function( another_data)
{
//make heavy computation on another_data
cache[data] = value;
// update the UI with value
});
}
else
{
//update the UI using cached value
}
}
})();

[#99567] Tuesday, May 5, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katelynn

Total Points: 378
Total Questions: 86
Total Answers: 108

Location: Azerbaijan
Member since Fri, May 12, 2023
1 Year ago
katelynn questions
Fri, Mar 12, 21, 00:00, 3 Years ago
Mon, Nov 16, 20, 00:00, 4 Years ago
Thu, Jul 23, 20, 00:00, 4 Years ago
Wed, Apr 15, 20, 00:00, 4 Years ago
;