简体   繁体   English

如何使用Laravel每隔X秒自动刷新div

[英]How to auto refresh a div every x seconds with laravel

This maybe a broad question but here is the thing. 这可能是一个广泛的问题,但这是问题。 I am using laravel and i have a database notification which displays the amount of unread badges (bootstrap badges) and a list of read and unread notifications. 我正在使用laravel,我有一个数据库通知,其中显示了未读徽章(引导徽章)的数量以及已读和未读通知的列表。 I want to be able to refresh only that div every 5 seconds or so. 我希望能够每5秒左右刷新一次该div。

Here is the code for database notification on my blade so far (admin-master.blade.php): 到目前为止,这是我的刀片上数据库通知的代码(admin-master.blade.php):

<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown">
    <i class="lnr lnr-bullhorn"></i>
    <span class="badge badge-light">{{\Illuminate\Support\Facades\Auth::guard('admin')->user()->unreadNotifications()->count()}}</span>
    <i class="icon-submenu lnr lnr-chevron-down"></i>
  </a>
  <ul class="dropdown-menu">
                            @foreach(\Illuminate\Support\Facades\Auth::guard('admin')->user()->unreadNotifications as $notifications)

    <li>
      <a class="unreadNotifications" href="{{route('receptionist.markasread', $notifications->id)}}">{{$notifications->data['data']}}</a>
    </li>
                            @endforeach
                                @foreach(\Illuminate\Support\Facades\Auth::guard('admin')->user()->readNotifications()->take(4)->get() as $notifications)

    <li>
      <a class="readNotifications" href="/admin/roomstatus">{{$notifications->data['data']}}</a>
    </li>
                                @endforeach

  </ul>
</li>

So my question is, how can i use JS with this laravel to refresh the whole div. 所以我的问题是,我如何在这个Laravel中使用JS来刷新整个div。 I want to be able to refresh both the badges and the actual notifications. 我希望能够刷新徽章和实际的通知。 I have seen some posts on stack overflow suggesting to use AJAX to "load" an html. 我已经在堆栈溢出中看到一些建议使用AJAX来“加载” html的帖子。 I am not sure how i could achieve that with laravel. 我不确定如何使用laravel来实现。

Thanks 谢谢

Ok so here is how i resolved the issue. 好的,这就是我解决问题的方式。

I used jquery ajax to fetch a route. 我使用了jQuery ajax来获取路由。 The route goes to a controller function which passes a view. 路径转到通过视图的控制器功能。 The original Questions code is in the view. 原始的Questions代码在视图中。

Here is my AJAX: 这是我的AJAX:

function loadlink(){
        $('#reload').load('/loadNotification');
        console.log('TESTING!!!!');
    }

    loadlink();
    setInterval(function(){
        loadlink()
    }, 10000);

So i refresh only that element id every 10 seconds. 因此,我每10秒仅刷新一次该元素ID。 Of course you could listen for any database changes as well if you don't want to use a timer. 当然,如果您不想使用计时器,也可以监听数据库的任何更改。 a timer in my case suited my needs. 就我而言,定时器适合我的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM