/    Sign up×
Community /Pin to ProfileBookmark

thread safe function (AJAX)

Hi All

in a loop I load, via AJAX, a couple of xml files using the same callback function
If all files are loaded my script continues to do other stuff

Basically my callback function does something like (I also use the prototype library)

[code]
MyClass.prototype = {
initialize: function() {…}, // load 10 files via AJAX (callback function is: my_callback)
…..
my_callback: function(file, …) {
this.count ++ ; // an other file loaded
…. do stuff … // at this point an other file might be ready and reach the previous line
if ( this.count == 10 )
this.do_other_stuff() ; // and this function can be called multiple times!!
},
do_other_stuff: function(){….}
}
[/code]

Do I not need to worry about thread safety in javascript ?

I ask this because I have strange problems at the moment, which might be explained by this. If I do need to worry about this, what would be the best approach in this situation ?

Thnx a lot
LuCa

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarJan 11.2009 — I guess most languages have javascript beat here, javascript is very multi-threaded, but it doesn't have semaphors or anything that that which would be really handy here.

I guess you could get around your problem with a little rewriting:
<i>
</i>MyClass.prototype = {
initialize: function() {...}, // load 10 files via AJAX (callback function is: my_callback)
.....
my_callback: function(file, ...) {
this.finished.push(file);
.... do stuff ...
if ( this.finished.length == 10 &amp;&amp; this.finished[9] == file)
this.do_other_stuff() ; // and this function can be called multiple times!!
},
do_other_stuff: function(){....}
}

Just make this.finished an empty array when you start, and it should all be good.
Copy linkTweet thisAlerts:
@jeanlucaauthorJan 12.2009 — thnx a lot!! that's a great solution!

cheers

LuCa
×

Success!

Help @jeanluca spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...