AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

Jquery ajax post with timer event?

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.

Upvotes: 0

Views: 1429

Answers (2)

Ionică Bizău
Ionică Bizău

Reputation: 113375

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);

Upvotes: 0

techfoobar
techfoobar

Reputation: 66663

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);

Upvotes: 3

Related Questions