croeltgen Posted November 21, 2010 Share Posted November 21, 2010 Hello,I have a javascript function running that does a potentially lengthy database update. When I call the function, I do:document.getElementById("upddb").value="..Updating database..";document.getElementById("upddb").style.opacity=0.5;(... do the updates...)return;However the effect in the browser window gets only visible when the function has completed!What can I do that the effect is shown in the beginning?Thank you Link to comment Share on other sites More sharing options...
thescientist Posted November 21, 2010 Share Posted November 21, 2010 I think what you are asking for is how to run the database update 'in the background.' You must using AJAX correct? Have you tried setting the async param of the open method to true? Link to comment Share on other sites More sharing options...
croeltgen Posted November 21, 2010 Author Share Posted November 21, 2010 I think what you are asking for is how to run the database update 'in the background.' You must using AJAX correct? Have you tried setting the async param of the open method to true?No, the database update must run in the foreground and all user actions must be ignored during the update. I want to create a visible effect for the user so that he is aware that he cant work until my js is finished Link to comment Share on other sites More sharing options...
thescientist Posted November 21, 2010 Share Posted November 21, 2010 oh, well this line from your first post However the effect in the browser window gets only visible when the function has completed!To me this is saying the visibility dims and then returns after the function has run. Perhaps you could post a link or some code, and provide some more details on the topic? Link to comment Share on other sites More sharing options...
croeltgen Posted November 21, 2010 Author Share Posted November 21, 2010 Here's what I do:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><script type="text/javascript">function update(){document.getElementById("upddb").value="..STARTING JAVASCRIPT..";document.getElementById("upddb").style.opacity=0.5;for (var l=0;l<1000000000;l++){}//document.getElementById("upddb").value="..ready..";//document.getElementById("upddb").style.opacity=1;}</script><body><div id="container"><form action="#"> <input id="upddb" type="button" onclick="update();" value="TEST A LONG JAVASCRIPT EXECUTION"/></form> </div></body></html>When you click on the button, the changes in the window are only done AFTER the for loop is executed.And what I want is that it is done BEFORE.Of course this code is nonsense - the loop only represents a long db update but it has the same effect. Link to comment Share on other sites More sharing options...
Ingolme Posted November 21, 2010 Share Posted November 21, 2010 It sounds like the browser isn't redrawing until the function finished being executed.I'm not certain if this will work for your case, but I heard that switching the display to none and then back will force the browser to redraw. element.style.display = "none";element.style.display = ""; Link to comment Share on other sites More sharing options...
croeltgen Posted November 22, 2010 Author Share Posted November 22, 2010 I tried document.getElementById("upddb").style.display = "none";but this has no effect.The browser always seems to wait the end of js execution before it updates its window.What helps is doing it in the following way:function update(){document.getElementById("upddb").value="..STARTING JAVASCRIPT..";window.setTimeout("nothing()",1);}function nothing(){...update runs here} Link to comment Share on other sites More sharing options...
justsomeguy Posted November 22, 2010 Share Posted November 22, 2010 I'm not sure how you're doing the update, but if you're using ajax then it will work to change whatever you want to change, set up the response handler, send the request, and have the response handler turn the effect off again. In other words, the code you left out is the important part, the stuff that you substituted with a loop. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.