/    Sign up×
Community /Pin to ProfileBookmark

Tiny timing problem

Hi.

I’m using mootools ajax. and I’ve coded something like this:

[CODE]<script type=”text/javascript”>
new ajax (“file.php”, {
postBody: “value=” + $(‘firstValue’).value + “&value2=” + $(‘secondValue’).value,
update: $(‘textPart’),
onComplete: $(‘theImage’).src = ‘images/’ + $(‘firstValue’).value + ‘.jpg’,
});
</script>[/CODE]

file.php makes an image for me and that image url must be replaced with <img id=”theImage” …

So far the file.php works fine and generates the image but it takes 1-2 sec to do it. my ajax script replaces the new image url fine but BEFORE the php creates the file.
So HTML fails to show image, because the image url is updated faster than the image itself is there.

It seems like, as soon as I click the element to call this ajax function, the image url gets updated, that doesn’t sound ‘onComplete’ to me.
Why onComplete is not actually waiting for the whole thing to get its thing finished? and how can I fix this?

Thanks for your time
p.s I put sleep(2000); in the end of the php file, but nothing changed. (I don’t want to hold php script for 2 sec either)

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameFeb 18.2010 — When you are defining onComplete you should do so as a function to be executed upon completion of the ajax request rather than a statement which is executed immediately as it is read. So try something more like:
onComplete:function () { $('theImage').src = 'images/' + $('firstValue').value + '.jpg'; }
Copy linkTweet thisAlerts:
@asmith20002authorFeb 18.2010 — Thank you!! Worked like a charm ?
×

Success!

Help @asmith20002 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...