-->

make ajax call to run multiple times with setInter

2019-09-13 07:34发布

问题:

I have an vb.net aspx page that loads a modal div on document.ready.

then I have a function that basically does an ajax call to an aspx page that returns date/time.

The ajax call then update a div in the modal div layer with the date and time

All the above works when the page initially loads.

BUT I want it to do that function every 5 seconds or so - but it only does it once.

so what I would like in summary is... - page loads - document.ready shows modal div (this works) - then setInterval to call ajax function to my page that returns date.time (this works 1 time)

  • then while the modal div is showing, and while on the page, setInterval to continually do the above line (this does not work)

without my giving any code (assume my code is crap for now) lol can this be done?

bascially, I want the page to simply display the date/time that updates a divs innetText every 5 seconds via calling ajax function.

Then I will expand on that if we can get it to work :)

Again any help greatly appreciated :)

Cheers

UPDATE 04-11-2013 10:04

I have just tried putting the setInterval call in the document ready block - and even added an alert to the function that I call to see if it actually calls it more than once - and it does not - still only once.

Here is the code.

so at the start of the script tags I have

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        showPayWait();
        setInterval(processPayment(), 5000);
    });        

    /* Function to Process Payment via AJAX calls so we can update the modal div */
    function processPayment() {

        // show modal div first
        var OrderIDResponse = "";
        var lngOrderID = "";
        UpdateMessage('Assigning an OrderID');

        // Get Ajax Response into variable.
        OrderIDResponse = getOrderID();

        // Parse it to the actual text we need i.e. the orderid
        lngOrderID = OrderIDResponse['responseText'];

        if (!lngOrderID == "") {
            UpdateMessage("OrderID Assigned: " + lngOrderID);

        }
        else {
            UpdateMessage('Unable to assign an OrderID ' + lngOrderID);
        }


    }

    // -- Generic function to update the divInfo Element
    function UpdateMessage(msg) {
        document.getElementById("divInfo").innerText = msg;
    }
</script>

Just thought I should point out that the page where this code is, is a content page of a masterpage

the master page has just the link to the latest jQuery include.

Thanks again for your replies - and as always any help will be greatly received.

Cheers

回答1:

Either do it like this:

function update() {
   //update logic belongs here

   setTimeout(update, 5000 /*5secs*/);
}

setTimeout(update, 5000 /*5secs*/);

or like this:

function update() {
   //update logic belongs here
}

setInterval(update, 5000 /*5secs*/);


回答2:

You need to use setInterval within the document ready function, like this:

$(document).ready(function() {
    setInterval(doAjax, 5000);
});

function doAjax() {
    // Call server-side resource here via AJAX
}

Note: If you want something else to trigger the 5-second interval, then put the setInterval(doAjax, 5000); call within whatever selector or click event handler you desire. It obviously does not need to be done with jQuery either, but that is the quickest example I could come up with.



回答3:

Got it working with a combination of your replies AND with an answer from another thread.

I was calling the function from setInterval WITH parentheses () and not just the function reference.

Also that combined with document ready = result!!!!

The answer I am referring to from another thread is JS setInterval executes only once