0

我有一个jTable,我想每 X 秒刷新一次。以前我有这个运行正确并进行了一些小的编辑,现在它不能正常运行,我无法确定我做了什么会导致这种情况。

这是我的jTable和启动/停止函数的代码:

<script type="text/javascript">
            var run=null;
            $(document).ready(function() {
                $('#TableContainer').jtable({
                    title: '',
                    actions: {
                        listAction: 'JSONCreator',
                    },
                    ajaxSettings: {
                        type: 'POST',
                        dataType: 'json'
                    },
                    fields: {
                        DeviceId: {
                            key: true,
                            list: false
                        },
                        DeviceTag: {
                            sorting: false,
                            search: true,
                            title: 'D',
                            width: '40%'
                        },
                        CH1: {
                            title: 'C1',
                            width: '10%'
                        },
                        CH2: {
                            title: 'C2',
                            width: '10%'
                        },
                        Timestamp: {
                            title: 'T',
                            width: '30%',
                            create: false,
                            edit: false
                        }
                    },
                });
                $('#TableContainer').jtable('load');
            });
            function startFunction() {
                document.title = '(Running)';
                run = window.setInterval("$('#TableContainer').jtable('reload')", 5000);
                window.open('M');
            }
            function stopFunction() {
                document.title = 'W';
                window.clearInterval(run);
                window.open('M');
            }
        </script>

然后在我的身体中有两个按钮后定义如下:

<input type="submit" value="Start" onclick="startFunction()"/>
<input type="submit" value="Stop" onclick="stopFunction()"/>

在这下面我有这条线

<div id="TableContainer"/>

现在它会在我按下任一按钮时执行一次该功能,但之后它不会继续重新加载表格。

window.setInterval("$('#TableContainer').jtable('reload')", 5000);

是什么导致这种情况发生?否则我该如何调试这样的东西?正如我之前所说,这之前运行正常

4

2 回答 2

1

You are not cancelling the submit action.

<input type="submit" value="Start" onclick="startFunction()"/>
<input type="submit" value="Stop" onclick="stopFunction()"/>

you need to return false or call preventDefault

<input type="submit" value="Start" onclick="startFunction(); return false;"/>
<input type="submit" value="Stop" onclick="stopFunction(); return false;"/>

You really should not be using inline events, You are using jQuery, utilize it!

$(window).on("click", "input[type=submit]", function(e) {
    e.preventDefault();
    if (this.value==="Start") {
        startFunction();
    } else {
        stopFunction();
    }
});

And for your setTimeout, you really should not pass in a string since that has to be evaluated every time it is called, pass in a function that is either named or anonymous.

于 2013-07-16T17:27:01.983 回答
0

您的代码格式错误。

window.setInterval("$('#TableContainer').jtable('reload')", 5000);

应该读

 window.setInterval(function(){$('#TableContainer').jtable('reload');},5000)
于 2013-07-16T17:15:35.157 回答