简体   繁体   English

JavaScript只工作一次

[英]JavaScript work only one time

I face with some problem. 我遇到一些问题。 I have page and make auto refresh div(table) content everything works fine and i have table with filters from javascript, but after refresh filters gone. 我有页面并使自动刷新div(table)内容一切正常,我有带有javascript过滤器的表格,但是刷新过滤器消失之后。 I try call again filters function but nothing happen. 我尝试再次调用过滤器功能,但没有任何反应。

Why dont work filterJS() again with setInterval script? 为什么不使用setInterval脚本再次运行filterJS()?

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Main Page</title> <script type="text/javascript" src="filter/tablefilter.js"></script> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <div class="refresh"> <table id="tab" class="my" > <tr> <th>World Regions</th> <th>Population ( 2007 Est.)</th> <th>Population % of World</th> <th>Internet Usage, Latest Data</th> <th>% Population ( Penetration )</th> <th>Usage % of World</th> <th>Usage Growth 2000-2007</th> </tr> <tr> <td>Africa</td> <td>933,448,292</td> <td>14.2 %</td> <td>32,765,700</td> <td>3.5 %</td> <td>3.0 %</td> <td>625.8%</td> </tr> <tr> <td>Asia</td> <td>3,712,527,624</td> <td>56.5 %</td> <td>389,392,288</td> <td>10.5 %</td> <td>35.6 %</td> <td>240.7 %</td> </tr> <tr> <td>Europe</td> <td>809,624,686</td> <td>12.3 %</td> <td>312,722,892</td> <td>38.6 %</td> <td>28.6 %</td> <td>197.6 %</td> </tr> <tr> <td>Middle East</td> <td>193,452,727</td> <td>2.9 %</td> <td>19,382,400</td> <td>10.0 %</td> <td>1.8 %</td> <td>490.1 %</td> </tr> </table> </div> <script language="javascript" type="text/javascript"> //<![CDATA[ function filterJS(){ var table2_Props = { col_0: "select", col_5: "none", display_all_text: " [ Show all ] ", sort_select: true }; setFilterGrid( "tab",table2_Props );} //]]> filterJS(); $(document).ready(function(){ setInterval(function() { filterJS(); $('.refresh').load(document.URL + ' .refresh');}, 2000); }); </script> </body> </html> 

Call it after the table is updated, use the callback argument that load() provides 在表更新后调用它,使用load()提供的回调参数

setInterval(function() { $('#myTable').load(document.URL +  ' #myTable', filterJS);}, 2000);

and you really should look into using setTimeout and not interval so if the server takes awhile to respond, the calls do not start to pile up. 您确实应该考虑使用setTimeout而不是使用interval,因此,如果服务器需要一段时间才能响应,则调用不会开始堆积。

function filterJS() {
    var table2_Props = {
        col_0: "select",
        col_1: "select",
        col_3: "none",
        display_all_text: " [ All ] ",
        sort_select: true
    };
    setFilterGrid("myTable", table2_Props);
    window.setTimeout(reloadData, 2000);
};

function reloadData() {
    $('#myTable').load(document.URL + ' #myTable', filterJS);
}

Set it up to call the function recursively in the callback, and use setTimeout instead of interval 设置它以在回调中递归调用函数,并使用setTimeout而不是interval

function getTable() {
  $('#myTable').load(document.URL + ' #myTable', function() {
    setTimeout(function(){
      getTable();
    }, 2000)
  });
}

If you are trying to delay the script to ensure the filters run AFTER the table is loaded, it will only work until your network is slow... so quite a bad way of doing it. 如果您试图延迟脚本以确保筛选器在表加载后运行,则它将仅在网络运行缓慢之前起作用...这是一种非常糟糕的做法。 Instead: 代替:

$(document).ready(function(){
      $('#myTable').load(document.URL +  ' #myTable', function() {
          filterJS();
      });
});

This way you use a callback function which will execute every time after the load is complete. 这样,您将使用一个回调函数,该函数将在加载完成后每次执行。

Add your recursion accordingly. 相应地添加您的递归。

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

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