imperium2335 Posted November 28, 2011 Share Posted November 28, 2011 I would like to know does mysql buffer queries? I ask because a while ago I remember an application I was developing wouldn't insert/update the database properly until I put a sleep(1); function between the two statements. I am concerned that the new webapp I'm creating will experience similar 'missed' queries because it will be doing a lot of inserts and update every second, perhaps up to a hundred per second. Quote Link to comment Share on other sites More sharing options...
requinix Posted November 28, 2011 Share Posted November 28, 2011 Unless you use a LOW_PRIORITY then queries will be executed in the order you give them. There could be a pause between them, but they won't go out of sequence. What does "wouldn't insert/update properly" mean? Didn't happen? Error messages? And why are you running a hundred of these per second? What is it doing? Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted November 29, 2011 Author Share Posted November 29, 2011 Simply wouldn't execute, no errors or anything. As for the new project there are going to be a lot of insert/update/deletes because there will be a lot of ajax going on inside setinterval. Quote Link to comment Share on other sites More sharing options...
requinix Posted November 29, 2011 Share Posted November 29, 2011 What kind of code are you executing? Anything you can post? I can't think of many situations where a sleep(1) would affect much, mostly involving caching at some layer. Aside: If you need to make sure that the AJAX calls happen in order then you shouldn't rely on setInterval(). While the code may be executed properly, the asynchronous calls may not complete in the same time. For instance, there may be some random hiccup that delays one long enough that another executes first. Best thing would be to design them so that order doesn't matter, but if that's not possible then consider using setTimeout() instead: once to execute and again at the end of the code to run. Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted December 1, 2011 Author Share Posted December 1, 2011 How would that be done like you say? What is the difference between having a function with settimeout call itself again and setinterval?? Quote Link to comment Share on other sites More sharing options...
requinix Posted December 1, 2011 Share Posted December 1, 2011 Using setInterval() the code will be executed periodically, even if the previous call hasn't completed yet. Which means it's possible that the second call executes before the first - and the odds increase if the interval is short. With setTimeout() you control when the next call happens. It's synchronous. var interval = 100; var keepworking = true; function work() { // ... if (keepworking) window.setTimeout(work, interval); } work(); // if you want it to start immediately, otherwise window.setTimeout(work, interval) There is another difference: with setInterval(code, X) your code will execute roughly every X milliseconds, but if you use setTimeout() then it'll execute slightly less often because you aren't accounting for the time to execute the code itself. Like if the code took 1 second (an exaggeration) and your interval was 0.1 seconds, it would actually be about 1.1 seconds between each call. Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted December 1, 2011 Author Share Posted December 1, 2011 Thanks requinix, So I assume the following will only do the next iteration once the php has finished processing?: var interval = 100; var keepworking = true; function work() { $.post('myphpfile.php', {stuff:stuff}, function(){ if (keepworking) window.setTimeout(work, interval); }) } Quote Link to comment Share on other sites More sharing options...
fenway Posted December 1, 2011 Share Posted December 1, 2011 Also, with setInterval, if the interval is really short, you can end up calling the function while the previous function call is still executing,. Quote Link to comment Share on other sites More sharing options...
requinix Posted December 1, 2011 Share Posted December 1, 2011 So I assume the following will only do the next iteration once the php has finished processing?: As long as myphpfile.php doesn't do anything special such as run itself in the background, yes. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.