简体   繁体   English

在分页页面上,如何阻止javascript自动加载?

[英]How do I stop javascript from autoloading when on a pagination page?

Basically, I have javascript refreshing a table of orders every 2 seconds. 基本上,我有javascript每2秒刷新一次订单表。 It queries a route called 'autoload' which triggers a method called autoloadOrders in the OrdersController. 它查询一个名为“ autoload”的路由,该路由会在OrdersController中触发一个名为autoloadOrders的方法。 The controller simply returns a view of a partial blade file with the loop for the table in it (with necessary variables being passed). 控制器仅返回部分刀片文件的视图,其中包含表的循环(传递了必要的变量)。

My question is, the refreshing works fine and the table auto-updates. 我的问题是,刷新工作正常并且表自动更新。 But for pagination, when I click to the next page, it auto-reverts back to the first page data every 2 seconds. 但是对于分页,当我单击到下一页时,它将每2秒自动恢复到第一页数据。 How can I stop that from happening once I'm going through the pagination? 分页后如何阻止这种情况发生?

welcome.blade.php welcome.blade.php

<script>
            $(document).ready(function() {
                setInterval(function() {
                    $('#autoload').load('{{ route('autoload') }}');
                }, 2000);
            });
        </script>
    <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
        <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
            <h1 class="h2">Dashboard</h1>
        </div>

        <h2>View Orders</h2>
        <div class="table-responsive" id="autoload">
            <table class="table table-striped table-sm">
                <thead>
                <tr>
                    <th>id</th>
                    <th>phone</th>
                    <th>address</th>
                    <th>order</th>
                    <th>price</th>
                    <th>delivered</th>
                </tr>
                </thead>
                <tbody>
                @foreach ($orders as $order)

                <tr>
                    @if ($order->delivered == false)
                    <td>{{$order->id}}</td>
                    <td>{{$order->phone}}</td>
                    <td>{{$order->address}}</td>
                    <td>{!! nl2br($order->products) !!}</td>
                    <td>${{$order->price}}</td>
                        @if ($order->pickup == true)
                            <td><a href="/admin/orders/pickup/{{$order->id}}">Pickup</a></td>
                        @else
                            <td><a href="/admin/orders/deliver/{{$order->id}}">No</a></td>
                            @endif

                     @endif
                </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        {{$orders->links()}}

autoload.blade.php autoload.blade.php

<div class="table-responsive" id="autoload">
    <table class="table table-striped table-sm">
        <thead>
        <tr>
            <th>id</th>
            <th>phone</th>
            <th>address</th>
            <th>order</th>
            <th>price</th>
            <th>delivered</th>
        </tr>
        </thead>
        <tbody>
        @foreach ($orders as $order)

            <tr>
                @if ($order->delivered == false)
                    <td>{{$order->id}}</td>
                    <td>{{$order->phone}}</td>
                    <td>{{$order->address}}</td>
                    <td>{!! nl2br($order->products) !!}</td>
                    <td>${{$order->price}}</td>
                    @if ($order->pickup == true)
                        <td><a href="/admin/orders/pickup/{{$order->id}}">Pickup</a></td>
                    @else
                        <td><a href="/admin/orders/deliver/{{$order->id}}">No</a></td>
                    @endif

                @endif
            </tr>
        @endforeach
        </tbody>
    </table>

OrdersController OrdersController

public function autoloadOrders(Orders $orders)
    {
        return view('admin.partials.autoload')->with(['orders' => $orders->getUndeliveredOrders()]);
    }

Route: 路线:

Route::get('autoload', 'OrdersController@autoloadOrders')->name('autoload');

I solved it with this 我用这个解决了

function getParameterByName(name, url) {
                if (!url) url = window.location.href;
                name = name.replace(/[\[\]]/g, "\\$&");
                var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                    results = regex.exec(url);
                if (!results) return null;
                if (!results[2]) return '';
                return decodeURIComponent(results[2].replace(/\+/g, " "));
            }
            $(document).ready(function() {


                var check = getParameterByName('page');
                if ((check === '1') || (check === null)) {
                    var autoLoad = setInterval(function() {
                        $('#autoload').load('{{ route('autoload') }}');
                    }, 2000);
                } else {clearInterval(autoLoad);}

            });

暂无
暂无

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

相关问题 单击触发 JavaScript 的链接时,如何阻止网页滚动到顶部? - How do I stop a web page from scrolling to the top when a link is clicked that triggers JavaScript? 调用JavaScript函数时,如何阻止网页滚动到顶部? - How do I stop a web page from scrolling to the top when calling a JavaScript function? 如何阻止 javascript 重定向页面? - How do I stop javascript from redirecting the page? 如何在页面呈现时停止 onClick 呈现? - How do I stop onClick from rendering when the page renders? 执行 C# 代码并注册 JavaScript 启动脚本时,如何停止页面刷新? - How do I stop a page refresh when C# code behind is executed and a JavaScript startup script is registered? 如何在页面加载时停止javascript中的警报触发,并在单击按钮时使其运行? - How can I stop an alert in javascript from firing when the page loads, and make it run when a button is clicked? 如何在页面加载时停止执行某个javascript - How do I stop the execution of a certain javascript on page load 在动画元素以使其从屏幕上滑入时,如何停止页面展开? - How do I stop the page from expanding when animating an element so it slides in from off screen? 当在PHP中单击按钮时,如何停止页面滚动到顶部? - How do i stop a page from scrolling to the top when button clicked in php? 单击选项卡时如何停止重新加载页面? (jQuery的) - How do I stop the page from reloading when clicking on tabs? (jQuery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM