jQuery, .empty() and memory

后端 未结 4 1525
广开言路
广开言路 2020-12-21 02:47

my application (ASP.NET MVC) shows a page which loads data constantly, at certain intervals.

The jQuery script calls a controller and this one renders a different pa

4条回答
  •  [愿得一人]
    2020-12-21 03:19

    You might want to try this instead:

    [EDIT] removed function being returned to setInterval

    $(document).ready(function() {
        $('#loading').html("").hide();
        ScheduledAction(LoadAppointments, -1, <%=Model.RefreshTimeout %>);
    });
    
    function ScheduledAction(func, times, interval) {
        var ID = window.setInterval(function() {
            if (times > -1) {
                if (--times <= 0)
                    window.clearInterval(ID);
            }
            func();
        }, interval);
    }
    
    function LoadAppointments() {
    
        $("#AppointmentsList").empty();
        $('#loading').show();
        $.get(UrlAction,
                function(data) {
                    if (data != '') {
                        $('#AppointmentsList').append(data);
                        $('#loading').hide();
                    }
                    else {
                        $('#loading').fadeOut(3000);
                    }
                });
    }
    

    I noticed that you were loading your spinner every time you were loading appointments. Also you were passing in times into the window.setInterval.

    My code testing this function is:

    $(document).ready(function() {
        $('#loading').html("").hide();
        ScheduledAction(LoadAppointments, 1, 100);
    });
    
    function ScheduledAction(func, times, interval) {
        var ID = setInterval(function() {
            if (times > -1) {
                if (--times <= 0)
                    clearInterval(ID);
            }
            func();
        }, interval);
    }
    
    function LoadAppointments() {
    
        $("#content").empty();
        $('#loading').show();
        $.get('contentServer.php',
                function(data) {
                    if (data != '') {
                        $('#content').append(data);
                        $('#loading').hide();
                    }
                    else {
                        $('#loading').fadeOut(3000);
                    }
                });
    }
    

    The php file:

    //contentServer.php
    
    
    

提交回复
热议问题