A New Use of Sleep() in PHP
Aug 28th, 2006
I have been working on a website that allows photo uploading and manipulation via a web interface. The webpage uses a hidden iframe so that uploading can take place without refreshing the page and AJAX that allows users to rotate and delete photos, again without refreshing the page. I am developing the application on my linux desktop and I thought it would be useful to simulate delays and normal response times if the application where on the internet, hence the use of the sleep function.
Nothing too spectacular here, but it is useful to throw sleep(10) into all your methods on the controller and see what happens. I found I had forgot to provide any user notification on delete of a photo. If the request was processed fast, as it always was on my desktop and usually was live on the internet, it wasn’t a problem because the JavaScript removed the photo. But if the request took 5 seconds to delete the photo, then the user would site there for 5 seconds wondering what was going on. Worse, they may hit the delete link again, causing another request to be sent, running a method to delete a possibly non-existent image. I guess the more I get used to AJAX and its idiosyncrasies, the less need I will have for this. But for now, it is a useful way to quickly simulate lag time on your desktop and ensure you are providing proper user notification.
On a different note, this application quickly became complex. I have wrapped all my JavaScript code into nice functions, but I was left wondering how I can better organize my code? Obviously using objects would help, and I need to become more familiar with JavaScript objects (It doesn’t seem worth the effort to just wrap the functions in an object). I was also left wondering what is the best way to organize code when your application becomes complex. Is the MVC paradigm somehow malleable to client side code? Something I need to investigate further….
Posted In: PHP, JavaScript