[英]setInterval is it the correct way to get notifications?
i need to create a messages notifications in my system built by yii2 , 我需要在yii2构建的系统中创建消息通知,
so i used this way 所以我用这种方式
1- i created in messages Controller function getnew : 1-我在消息控制器功能getnew中创建了:
public function actionGetnew()
{
$query1 = new Query;
$query1->select([" count(*) as price "] )
->from('messeges');
//->where // here i will add the id of user that logged in and the state of unread messages !
$command1 = $query1->createCommand();
$price = $command1->queryAll();
echo Json::encode($price);
}
2- i created a js file : 2-我创建了一个js文件:
window.onload = function() {
setInterval(function(){
$.get('index.php?r=messeges/getnew',function(data)
{
var data=$.parseJSON(data);
$(".messages").html(data[0].price);
});
}, 5000);
};
3- i added HTML tag like this : 3-我添加了这样的HTML标签:
<h6 class="messages"></h6>
everything work fine but my question is that right way to do it ? 一切正常,但是我的问题是正确的方法吗? , is that will add more traffic and load on my server ?
,这是否会增加服务器的流量和负载? if there is a better idea please notify me !
如果有更好的主意,请通知我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.