0

I want to check if my user is updated from another system using javascript.

I need help writing a function which checks a json response. If it is true or false.

The url /user/updatecheck/ has a json response like this: {"updated": "true"} or {"updated": "false"}

<script type="text/javascript">

$(document).ready(function() {
    var updated='2013-01-02T10:30:00.000123+02:00'; //user variable from the system, will be empty if the user is not updated       

    if (!updated){
        $('#not-updated').modal('show');

        var updatedCheck = window.setInterval(    
        $.ajax({
                    url: "/user/updatecheck/", //Returns {"updated": "true"} or {"updated": "false"}
                    data: dataString,
                    dataType: "json", 
                    success: function(data){ 

                       if (json.updated == 'true') { //not sure if this is the correct method
                           window.clearInterval(updatedCheck);
                           //The user is updated - the page needs a reload
                       }
                    } //success

                })
        , 3000);  //Hoping this is the function to check the url every 3 seconds until it returns true
    }

}); 
$(document).ajaxStop(function(){
    window.location.reload();
});

</script>

It does not seem to work. Not sure if my ajax function is correct, I only get the modal window if the user is not updated at first, and the page does not reload if the url /user/updatecheck/ returns true.

Tomas Jacobsen
  • 2,368
  • 6
  • 37
  • 81

1 Answers1

0

The way you call jQuery ajax function as part of the setInterval is not proper. Please try placing it within a function,

var updatedCheck = window.setInterval(    
        function(){$.ajax({
                    url: "/user/updatecheck/", //Returns {"updated": "true"} or {"updated": "false"}
                    data: dataString,
                    dataType: "json", 
                    success: function(data){ 

                       if (json.updated == 'true') { //not sure if this is the correct method
                           window.clearInterval(updatedCheck);
                           //The user is updated - the page needs a reload
                       }
                    } //success

                })}
        , 3000);

The data that you will receive from the ajax request is returned via the data parameter of you success callback function, so you can use that. Try to print the data result to the console (i.e. console.log(data);)or alert it (i.e. alert(data);). For instance you may need to call data.updated. I'm not sure if the json variable you are using in the if condition is initialised.

melc
  • 11,523
  • 3
  • 36
  • 41
  • Yes. This works after I removed `data: dataString` and changed `json.updated` to `updated` – Tomas Jacobsen Oct 25 '13 at 15:03
  • @Garreth00 you can use a counter and only check for specific number of times example, http://stackoverflow.com/questions/2956966/javascript-telling-setinterval-to-only-fire-x-amount-of-times . Or you can use a Date variable and check for a specific amount of time. – melc Oct 25 '13 at 15:38