gpt4 book ai didi

javascript - SetTimeout() 导致 cpu 使用率过高

转载 作者:搜寻专家 更新时间:2023-10-31 21:59:05 29 4
gpt4 key购买 nike

我试图通过在 PHP 页面中使用警报来显示来电,为此我需要每隔一秒检查 mysql 表是否存在来电。

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 操作:

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;
}

上面的查询消耗 CPU 使用率很高,请任何人指导我减少 CPU 负载!!

最佳答案

永远不会满足条件 idleTimer != null。将变量 idleTimer 移出函数范围,这样每次函数执行时都不会创建变量(并初始化为 null)。像这样:

 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();

关于javascript - SetTimeout() 导致 cpu 使用率过高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31019005/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com