Jump to content

jQuery - can't seem to figure out why functions aren't running independently


micah1701

Recommended Posts

I have a function in Javascript that uses jQuery's .get() function to tell my PHP script to update a $_SESSION value.  it looks something like this:

function updateSession(key,value){
$.get('updateSessionArray.php',{key:key,value:value});
}

 

(the PHP code is inconsequential and works fine but I'll post it here so you get what I'm doing, it looks something like:

<?php $_SESSION[$_GET['key']] = $_GET['value']; ?>

 

This all works fine.  When I call something like  updateSession("username","Joe"); in JavaScript the $_SESSION array is updated and everyone's happy.

 

Here's where my problem starts

 

When I try to call the function multiple times at once it doesn't work.  for example, I have a function that needs to update the $_SESSION several times, sort of like this:

function createUser(){
updateSession("username","joe");
updateSession("favorite_color","blue");
updateSession("foo","bar");
} 

 

But when i try this, the original function only runs once - sometimes twice.  So in the above example, the username would be set to joe and the favorite_color would be set to blue but foo would not be set to bar.  Or perhaps it would but color wouldn't be set to blue.  Or Only the username gets set.

 

It would seem that once the function is busy running (and possibly waiting for a reply) it won't run again.  Is that the correct behavior?  and if so, is it a javaScript specific behavior or a jQuery thing.  It seems odd though as, I'm not asking for it to wait for a response.  I just want it to trigger the function to run independently multiple times.

 

I originally wrote this script without jQuery and I never had a problem before.  I admit though that I'm no expert so maybe I'm missing something?  Am I just not wrapping my head around the way this should work?

 

Any thoughts or comments would be appreciated.

Link to comment
Share on other sites

UPDATE:  The PHP script that I'm linking to (in my example it was "updateSessionArray.php"  has been running inside a CakePHP invornment.

 

I moved it out of cake and, not only does it run faster for all ajax actions but it seems to be working fine now.  When running in Cake it was taking a full 1 to 3 seconds to get my results back but now its near instant.

 

So my original problem still stands (as I'd like to keep the PHP page in the cake framework) but I think this helps narrow down the problem to one of two things:

 

Either 1) Its either CakePHP -- either putting some sort of limit on the number of requests or perhaps trying to cache results?

 

or 2) its a jQuery/JavaScript issue having to do with the amount of time it takes to process the .get() function while waiting for a long response.

 

Any thoughts in light of this revelation? 

Link to comment
Share on other sites

In light of my last post, I added a php sleep() call to function to purposely slow it down by several seconds and it still worked fine - showing there's no correlation between jQuery/javaScript and the speed it takes PHP to process the .get() request (which I suspected).

 

Thus, this must be a CakePHP issue.  Topic Closed.  Thanks for letting me think "out loud" by posting this here.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.