Question

Here is what I've got:

 <body>
        <div id="TableContainer"></div>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#TableContainer').jtable({
                    title: 'title',
                    actions: {
                        listAction: 'JSONServlet',
                    },
                    ajaxSettings: {
                        type: 'POST',
                        dataType: 'json'
                    },
                    fields: {
                        Id: {
                            key: true,
                            list: false
                        },
                        Tag: {
                            title: 'Tag',
                            width: '40%'
                        },
                        V: {
                            title: 'V',
                            width: '10%'
                        },
                        S: {
                            title: 'S',
                            width: '10%'
                        },
                        Timestamp: {
                            title: 'Timestamp',
                            width: '30%',
                            create: false,
                            edit: false
                        }
                    }
                });
                $('#DeviceTableContainer').jtable('load');
      ///////     window.setTimeout("$('#DeviceTableContainer').jtable('reload')", 100);
            });

        </script>
    </body>

I am using a jTable and everything works fine besides my setTimeout method. In firebug it looks like it is able to make a request twice. Once being the normal request and another from my method I assume but after the second time there are no more requests. I'm new to web development so if you could give me a quick explanation as to what I'm doing wrong that would help also.

Était-ce utile?

La solution

if you are wanting it to refresh every x seconds then you need setInterval or another call to setTimeout in the the function that gets called by setTimeout

var timerID = setInterval("$('#DeviceTableContainer').jtable('reload')", 100);

OR

var timerID = null;
function doReload() {
     $('#DeviceTableContainer').jtable('reload');
     timerID = setTimeout(doReload,100);
}

doReload();

use timerID to save a reference to the timer and cancel the timer if needed, clearTimeout(timerID), clearInterval(timerID)

Although you have it being called every 0.1 seconds, as the number is for milliseconds, so that is quite quick for doing ajax calls.

Autres conseils

setTimeout only fires once.

setInterval fires repeatedly until you call clearInterval

Try this (after 100ms):

window.setTimeout(function(){$('#DeviceTableContainer').jtable('reload');}, 100);

Or this (every 100ms):

window.setInterval(function(){$('#DeviceTableContainer').jtable('reload');}, 100);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top