Periodically send ajax request to server from JavaScript

<script>
    function executeQuery() {
        $.ajax({
            url: 'http://localhost:54802/Calls/CheckNewVoices/',
            type: 'POST',
            data: "{'lastcheckdatetime':'" + lastCheckDateTime + "'}",
            dataType: 'json',
            contentType:'application/json; charset=utf-8',
            success: function (response) {
                lastCheckDateTime = response.lastcheckdatetime;
                console.log(lastCheckDateTime);
                var files = response.files;
                files.forEach(function (file, index) {
                    //Do something
                });
                setTimeout(executeQuery, 30000); //Call after 30 seconds.
            }
        });
    }

    $(function () {
        setTimeout(executeQuery, 30000); //Call after 30 seconds.
    });
</script>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s