Question

I have an ajax call wrapped into a function in order to do polling which is

function doPoll(){
        alert('GO POLL')
        setTimeout(function(){
            $.ajax({
                'method':'POST',
                'url':'/ajax/request_news',
                'beforeSend':function(){
                    alert('Before send')
                    date = new Date()
                    var timestamp_last_check = Math.floor(date.getTime()/1000)
                },
                'data':{'last_check':timestamp_last_check}, 
                'success':function(ret){
                    ret = $.parseJSON(ret)
                },
                'complete':function(){
                    doPoll()
                }

            })
       },5000)
    };

This request doesn't work because it says that the var timestamp_last_check is not initialized. I though that putting this in beforeSend would work but it seems that the data context is retrieved before the beforeSend call.

I can't put my timestamp initialization outside the AJAX call because there's the setTimeout who will cause a 5 seconds delay between the initialization of the timestamp and the AJAX call.

I saw that there's a $.ajaxSetup function but the doc recommend not to use it.

What's the best practice to initialize this timestamp_last_check var ?

Thanks !

Was it helpful?

Solution

Try

setTimeout(function () {
     var timestamp_last_check = 0; //declare here to make it available to data:
     $.ajax({
         'method': 'POST',
             'url': '/ajax/request_news',
             'beforeSend': function () {
             alert('Before send')
             date = new Date()
             timestamp_last_check = Math.floor(date.getTime() / 1000); //set Value here
         },

Problem

var timestamp_last_check is local to the beforeSend: only so it is not accessible outside the block.

'beforeSend': function () {
                 alert('Before send')
                 date = new Date()
                 var timestamp_last_check = Math.floor(date.getTime() / 1000)
             },

Read What is the scope of variables in JavaScript?


Updated After OP's comment

function doPoll() {
    alert('GO POLL');
    setTimeout(function () {
    var date = new Date();
    var timestamp_last_check = Math.floor(date.getTime() / 1000);//set value here
        $.ajax({
            'method': 'POST',
                'url': '/ajax/request_news',
                'data': {
                'last_check': timestamp_last_check
            },
                'success': function (ret) {
                ret = $.parseJSON(ret)
            },
                'complete': function () {
                doPoll()
            }
        })
    }, 5000)
};

OTHER TIPS

function doPoll() {
    alert('GO POLL')
    var timestamp_last_check;
    setTimeout(function () {
        $.ajax({
            'method': 'POST',
            'url': '/ajax/request_news',
            'beforeSend': function (xhr, settings) {
                alert('Before send')
                date = new Date()
                timestamp_last_check = Math.floor(date.getTime() / 1000);
                settings.data.last_check = timestamp_last_check;
            },
            'success': function (ret) {
                ret = $.parseJSON(ret)
            },
            'complete': function () {
                doPoll()
            }

        })
    }, 5000)
};

Notice that I have declared timestamp_last_check in the scope of doPoll method. This makes it closure for the beforeSend. And the beforeSend accepts two arguments. The second argument is the settings object which should be used to change the settings or add any data in the request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top