会话值未在JOOMLA中的while循环内更​​新

I'm not very sure if this is Joomla or PHP issue in my code. Basically I've two functions, A and B that invoked on page load and onclick event respectively. On page load, I set session value to loadA. Once click event triggered, I set session value to clickB.

Meanwhile the while loop inside function is running until the condition is met. Inside the loop, it's checking for session value. So if the value updated inside function B, it should also be updated in function A when the while loop check right?

But it's not giving the updated value. Function A still gives loadA as session value inside while loop even after click event took place. How to fix this please?

class XXY(){

        public function A()
        {

            $session = JFactory::getSession();
            //set session value on page laod  
            $this->setSession('loadA');
            while (x == y)
            {
              usleep(10000);
              clearstatcache();
              $comet = $session->get('runningComet');//giving the first set value, loadA
              if($comet !== 'loadA'){
                break;
              }
            }

        }

        public function B()
        {
            $session = JFactory::getSession();

            $this->setSession('loadB');
            $comet = $session->get( 'runningComet');//giving the updated value, loadB
        }

        public function setSession($currentComet){

        $session = JFactory::getSession();
        $session->set('runningComet', $currentComet);
        }
    }