skip to Main Content

I wish to create a set interval of x milliseconds in the console logging of data. Any ideas on the code I will need to do this? Here is my code:

// API 
var apiUrl              =   'http://theurl.com';

// AJAX
jQuery.ajax ({
    url: apiUrl,
    type: 'GET',
    dataType: 'json',
    success: function(data) {

        console.log(data);

        // COORDINATES
        var coordLat            =   data.coord.lat;
        var coordLng            =   data.coord.lon;
        

2

Answers


  1. I wish to create a set interval of x milliseconds in the console logging of data.

    If you wish to set an interval when logging data to the console,
    you should neither need to use Ajax nor jQuery.

    Instead, you can achieve this with a single line of javascript, using the setInterval method.

    The example below logs a message to the console every 900 milliseconds.

    Working Example:

    let myInterval = setInterval(() => console.log('Logging...'), 900);
    Login or Signup to reply.
  2. Looking at your code I think you want to update data periodically.

    So this is an example based on your code:

            function getCoordinates()
            {
            // API 
            var apiUrl              =   'http://theurl.com';
    
            // AJAX
            jQuery.ajax ({
                url: apiUrl,
                type: 'GET',
                dataType: 'json',
                success: function(data) {
    
                    console.log(data);
    
                    // COORDINATES
                    var coordLat            =   data.coord.lat;
                    var coordLng            =   data.coord.lon;
                    }
                    }
                    );
    
               }
    
               var x = 10000;
               var interval = setInterval(()=>{
                    getCoordinates();
               },x);     
    
               ///.. don't forget to clear the interval when it's not necessary anymore
                clearInterval(interval);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search