繁体   English   中英

无限的javascript while循环查询表

[英]infinite javascript while loop to query table

在下面的标记上 - while循环在刷新页面时工作,即(回发) - 即while循环中的调用正在运行。 但是,如果没有刷新,它就不会更新(例如,如果我更新数据库中的值,我看不到网页上的更改)。 我希望它能不断获取数据。

为什么不,修复是什么?

MARKUP

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>TestPage</title>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="js/data-import.js"></script>
<script src="js/underscore-min.js"></script>
<script src="js/backbone-min.js"></script>
<script src="js/jquery.dateFormat-1.0.js"></script>
</head>
<body>
    <div id="container">
        <div id="header">
        </div>
                <div id="tabContent">
                    <div id="fileRepositoryTable">
                        <h2 id="dataImportHeader">File Management</h2>
                        <table width="100%">
                            <tr>
                                <td>
                                    <div id="fileRepository">
                                        <script type="text/template" id="fileRepository-template">
                                                <div id="fileRepositoryTable">

                    <h2 id="dataImportHeader">File Management</h2>
                    <table width="100%">
                        <tr>
                            <td>
                                <div id="fileRepository">
                                    <script type="text/template" id="fileRepository-template">
                        <table id = "fileRepositoryTable">
                            <thead>
                                <tr>
                                    <th>File Name</th>
                                    <th>Progress</th>
                                    <th>Time Imported</th>
                                </tr>
                            </thead>
                        <tbody>

                        </tbody>
                        </table>
                        </script>
                        <script type="text/template" id="fileRepositoryrow-template">
                        <td><%= get('fileName')%></td>
                        <td><%= get('progress') +" %"%></td>
                        <td><%= $.format.date(get('timeImported'), 'dd/MM/yyyy hh:mm:ss') %></td>
                        </script>
                        </div>
                        </td>
                        </tr>
                    </table>
                    </div>

* 骨干代码*

DataImport.FileRepositoryView = Backbone.View.extend({


    el: '#fileRepository',
    template: _.template($('#fileRepository-template').html()),

      initialize: function () {
        _.bindAll(this, 'render', 'appendRow');
        this.collection.bind('reset', this.render);
      },

      appendRow: function (fileRepo) {
        var repositoryView = new DataImport.FileRepositoryRowView({model: fileRepo});
        this.$el.find('tbody').append(repositoryView.render().el);
        return this;
      },

      render: function () {
        this.$el.html('');
        this.$el.append(this.template(this));
        _(this.collection.models).forEach(this.appendRow, this);
        return this;
      }
});

DataImport.FileRepositoryRowView = Backbone.View.extend({

     tagName: 'tr',
      template: _.template($('#fileRepositoryrow-template').html()),

      initialize: function () {
        _.bindAll(this, 'render');
        this.render();
      },

      render: function () {
        this.$el.html(this.template(this.model));
        return this;
      }
});

DataImport.FileRepositoryRows = Backbone.Collection.extend({

    model: DataImport.Row,
    url: DataImport.rootURL + 'path/getFileRepository',

});

* 编辑包括骨干代码*

代替

while (true) {}

尝试

setInterval(function() {
    // update code here
}, 0);

否则,由于JS经常使用CPU,您的浏览器将不会更新页面。

如果你必须使用

while (whatever) {}

在很长一段时间内,你应该被诱惑使用

var clock = setInterval(function() {
    // the code to repeat
    if (!whatever) {
        clearInterval(clock);
    }
}, 0);

相反,浏览器保持响应。

解决了:

在我的document.ready函数中:

setInterval(function() {
            DataImport.FileRepositoryRowList.fetch();
            }, 10000);

谢谢你们的帮助。

暂无
暂无

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

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