SetTimeout () causes high cpu usage

I am trying to show an incoming call using a warning on a PHP page, for that I need to check the mysql table if the incoming call exists or not every second.

Ajax:

function checkPushTask()
 {
     var idleTimer = null;
     $.ajax({
         type: "POST",
         url: "<?php  echo  CController::createUrl('/site/checkPushTask'); ?>",  
         async:true,
         success: function(msg){
             if(msg != -1)
             {  
                if (idleTimer != null)
                     window.clearTimeout(idleTimer);
                $(".TaskDisplay").html(msg);        
                idleTimer = setTimeout(checkPushTask, 3000);        
             }

             },
         error: function(){

         }
       });
 }
 checkPushTask();

      

Controller action:

public function actioncheckPushTask()
    {

        $criteria = new CDbCriteria();
        $criteria->alias = 'qws';
        $criteria->join = 'inner join crm_base_contact on crm_base_contact.crm_base_contact_id = qws.crm_base_contact_id';
        $criteria->condition = 'qws.crm_base_contact_id in (Select crm_base_contact_id from crm_campaign where is_active = 1 and status < 2 and crm_campaign_id in 
                                (select crm_campaign_id from crm_campaign_agent where g_user_id = '.yii::app()->user->getUserID().'))';
        $count = QueryWSModel::model()->count($criteria);
        if ($count > 0)
        {
            $cuser = CRMUser::model()->findByAttributes(array('g_user_id'=>yii::app()->user->getUserID()));
            $pendingmodel = PendingTaskModel::model()->find(array( 'condition' => 'FIND_IN_SET(:QueryWS_agent_id,QueryWS_agent_id)',
                                                                                'params' => array( ':QueryWS_agent_id' => yii::app()->user->getUserID())));
            if (sizeof($pendingmodel) > 0 && $cuser['current_task_id'] == null)
            {
                $user = Yii::app()->getComponent('user');
                $user->setFlash(
                'info',
                CHtml::link('Incoming Call: OpenTask #'.$pendingmodel->crm_task_id,$this->createUrl('operator/TaskOpen',array('id'=>$pendingmodel->crm_task_id)),array('style'=>"color:white"))
                );
                ob_start();
               $this->widget('bootstrap.widgets.TbAlert', array(
                'fade' => true,
                'closeText' => '&times;', // false equals no close link
                'events' => array(),
                'htmlOptions' => array(),
                'alerts' => array( 
                'info' => array('closeText' => '&times;'),
                ),
                ));
                $tab1Content= ob_get_contents();
                ob_end_clean();
                echo $tab1Content;
            }
        }
        else 
            echo -1;
    }

      

Above query is CPU intensive, please someone help me to reduce CPU usage!

+3


source to share


1 answer


The condition is idleTimer != null

never met. Move the variable idleTimer

out of scope, so the variable is not created (and not initialized until null

) every time the function is executed. Like this:



 var idleTimer = null;
 function checkPushTask()
 {
     $.ajax({
         type: "POST",
         url: "<?php  echo  CController::createUrl('/site/checkPushTask'); ?>",  
         async:true,
         success: function(msg){
             if(msg != -1)
             {  
                if (idleTimer != null)
                     window.clearTimeout(idleTimer);
                $(".TaskDisplay").html(msg);        
                idleTimer = setTimeout(checkPushTask, 3000);        
             }

             },
         error: function(){

         }
       });
 }
 checkPushTask();

      

0


source







All Articles