I have this $unreadMessageCount
variable in my view:
<a href="javascript:void(0)" id="myAccount" class="site-nav-sub"><i class="fa fa-angle-down site-nav-arrow"> </i>Hi, {{ firstname }} {% if unreadMessageCount > 0 %} <span class="badge pull-down" style="background: rgb(36, 232, 0); padding: 5px 1px;"> </span>{% endif %}</a>
In my ajax:
window.setInterval(function(){
$.ajax({
url: '/index/retrieveUnreadCount',
success: function(data){
console.log(data);
}
});
}, 30000);
In my controller:
public function retrieveUnreadCount()
{
if ($this->component->user->hasSession()) {
$this->user = $this->component->user->getSessionUser();
$this->view->setVar('user', $this->user);
$this->view->setVar('unreadMessageCount', Messages::getTotalUnreadCount($this->user->id));
$unreadMessageCount = Messages::getTotalUnreadCount($this->user->id);
return json_encode($unreadMessageCount);
}
}
I want to refresh $unreadMessageCount
every 30 seconds using Ajax as seen above but I am not sure if this code works.