0

script

$(document).ready(function () {
    var meter_id = $("#MeterReadingTypes li a.link_active").attr("id");
    var range_id = $("#DateRangeTypes li a.link_active").attr("id");

    window.setInterval(PostMainChartValues(meter_id, range_id), 5000);
    ...
});

function PostMainChartValues(meter_id, range_type_id) {
    $.ajax({
        ...
    });
}

window.setInterval is not trigerred. If I write an alert in setInterval it works. What is the reason of this? Why function is not triggering? I tracked it with chrome DevTools, and there is no move.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197

2 Answers2

3

The first parameter to setInterval should be a function (or an evalable string). Right now, you are calling PostMainChartValues() and passing its return value to setInterval().

Change it to:

window.setInterval(function() {
    PostMainChartValues(meter_id, range_id);
}, 5000);
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • Or SO's websockets notified me a few milliseconds earlier. :) Unlikely that I'm faster. :-) – techfoobar May 24 '13 at 09:13
  • It works, but I cant understand fully. What is the difference with [THIS](http://stackoverflow.com/questions/2295049/call-js-function-using-jquery-timer) – AliRıza Adıyahşi May 24 '13 at 09:13
  • @AliRızaAdıyahşi: In your example you're assigning the _returned value_ not the function it self as `setInterval` expects. – elclanrs May 24 '13 at 09:14
0

This is not an ajax issue. You are using in wrong mode the setInterval parameter.

Create an anonymous function like bellow:

window.setInterval(function () { PostMainChartValues(meter_id, range_id); }, 5000);
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474