Jump to content

Can jQuery load faster than PHP?


Mudsaf

Recommended Posts

Hello, i'm wondering how my jQuery loads faster than my php-script and how to prevent that kind of action. At jQuery i'm using function.

$(function() {//Document ready});

At my page i have this session unset script

if (isset($_SESSION['<session>'])) {unset($_SESSION['<session>']);}

And below it

$(function() {$.get('<mypage.php>', function(data) {//Blablabla});});

Most of times the session displays non-exist but sometimes it exists

 

<mypage.php>

if (isset($_SESSION['<session>'])) {echo "Positive"; //This happens like 1-12 refreshes} else {echo "Negative"; //This happens like 11-12 refreshes}
Link to comment
Share on other sites

Javascript runs as it loads, the session only saves its value when the PHP script finishes running, so Javascript starts executing as PHP is running. If you use session_write_close() right after unsetting the value it should fix the problem. You won't be able to change any other $_SESSION values after that, though.

Link to comment
Share on other sites

Javascript runs as it loads, the session only saves its value when the PHP script finishes running, so Javascript starts executing as PHP is running. If you use session_write_close() right after unsetting the value it should fix the problem. You won't be able to change any other $_SESSION values after that, though.

 

I kind a need the sessions afterwards.

 

 

Can't you set jQuery to start after the document has rendered except for the images that are loading?

 

Isn't $(function()) {}); doing it already?

Link to comment
Share on other sites

If you mean you still need the session variables in the same request, then add it right after you done messing with it "in that request". session_write_close doesn't delete the session or it's contents, it tells PHP that the page that accessed it is "done playing with it" so that other pages can use it. PHP only allows one page to work on specific session data at a time and locks other pages out of it (forcing them to wait in a sort of queue), at least it should. what you're getting is possibly a deadlock situation were changes in one page request is affecting some other, different page request before both requests even finish.

 

$(function(){}) runs the function immediately. People write it inside "$()" so that the function will run only once (never more) and doesn't pollute the global space. Use "$(document).ready(function(){})" for code that you want to run as soon as the DOM is ready.

 

Also, in php I used to accumulate HTML code into Heredoc syntax or something similar and then echo the result at the end, just so the server can process everything (or at least the essential things) before even sending anything to the browser. Since then I moved to MVC Frameworks like Zend where I didn't need to worry about such things.

Link to comment
Share on other sites

Ah fixed, added if session exist & pasted the jQuery after the check

<?phpif (isset($_SESSION['<session'])) {unset($_SESSION['<session']);?>$.get('<mypage.php>', function(data) { }); //Blablabla<?php} else {?>$.get('<mypage.php>', function(data) { }); //Blablabla<?php}?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...