JavaScript/jQuery check if file is available to read before trying to load/open
I have a PHP file that writes a significant amount of data to a "dump" file, then I am using Jquery to $('#element').load()
that data from my user interface page. The problem is that I am creating this dump file on the fly and as more and more of the dump file contents are ad开发者_如何学Cded, every few seconds my Jquery loads that data again. This works fine most of the time. The problem arises when the read happens, while the file is being written to. This halts my PHP script. I check in PHP with is is_writable()
to make sure the file is available but is there a "is_readable()
" type of function for JavaScript/Jquery? I know that I could create an "intermediate" PHP file to check, but that would require a significant amount of change to do, is there an easy way to check in JavaScript?
In your ajax call, you should just request the file and if it's not available, you should have an error handler that does whatever you want to do when it's not available. There is no other way to preflight it unless you invent a different ajax call on your server to give you that info.
If asking for it when it doesn't exist causes your PHP script a problem, then you need to just figure out how to fix that issue so it can just return either empty data or just return an error to the ajax call without messing up the server.
Ending up having to use an intermediary php file that waits for the dump file to become available before trying to read from it, then just echos the contents
精彩评论