Avatar of Øyvind Isaksen
Øyvind Isaksen
Flag for Norway

asked on 

Ajax run each second, get data from WebMethod

I have simple WebMethod that do some job and returns status. In my example, it only returns a number for testing.

Then I have an Ajax code that post to my webmethod. If I call the WebMethod directly, it returns data correct. But, if I call the WebMethod from inside setInterval(), the WebMethod don't return data. If I try to return a variable from inside the WebMethod, it works correct.

How can I run a WebMethod each second, until the WebMethod returns wanted result?

<script type = "text/javascript">
    var counter = 5;

    var Pull = function (c) {
        var data = JSON.stringify({ count: c });

            type: "POST",
            url: "Accepted.aspx/Receipt",
            contentType: "application/json; charset=utf-8",
            data: data,
            dataType: "json",
            success: PullSuccess,
            error: OnError


    function PullSuccess(response) {
        $("#<%= lbl.ClientID %>").text(response.d);

    function OnError(response) {
        $("#<%= lbl.ClientID %>").text("Error occured");

    function countdown() {
        var interval = setInterval(function () {

            if (counter <= 0) {
                $("#<%= lbl.ClientID %>").text("Done");
            } else {


        }, 1000);


    // Works fine:
    // Pull(5);


public static int Receipt(int count)
    return count;

Open in new window


Avatar of undefined
Last Comment
Øyvind Isaksen

8/22/2022 - Mon