Search for data in Json and display time in time and move on to javascrtipt

Asked

Viewed 233 times

0

I need to get the data that are in json format example:

{"latitude": "-3,3462, -60,6790"}

I need to display the results time in time type every 5 seconds and move to javascript without refreshing the page.

javascript

var testMarker = L.marker([-3.3462, -60.6790],{icon: orangeIcon}).bindPopup('Kedson<br>Visto: 12:00pm');

  • the view you refer to is to return to a websocket, or ajax, or you are using CLI and need to keep checking a variable in some thread?

  • In javascript it looks like this: var testMarker = L.Marker([-3.3462, -60.6790],{icon: orangeIcon}). bindPopup('Kedson<br>Seen: 12:00pm');

  • I need to change these values -3.3462, -60.6790 every 5 seconds and latitude

  • So you have a javascript variable that needs every 5 seconds to be updated with the latest php service value, this? is using some framework in javascript or php?

  • I will put the javascript code below the question

  • This object you placed from the coordinates, is that what it is? With the value of latitude and longitude with comma in the decimal separator and still separated by comma, all within a string? You cannot return something like PHP {"latitude": -3.346, "longitude": -60.6790} or {"coord": [-3.3462, -60.6790]} ?

  • {"latitude": -3.346, "longitude": -60.6790}

Show 2 more comments

2 answers

0

You can make an ajax query that at the end of the query it launches a setTimeout with the time for the next update, the code with jquery would look something like this :

function updateData(){
  $.post( "/echo/json/",{}, function( data ) {
    /* Atualiza os dados */
    setTimeout(updateData,5000);
  });
}
updateData();
  • In case the page updates itself ?

  • This, after calling the function the first time, it will call every 5 seconds

0

Using the $.getJSON jQuery

You can use code similar to the one in which you put a timer to update every 5 seconds, which will fetch a JSON via ajax and update something via JS when it returns.

setInterval( function () {
  $.getJSON("endereco_da_pagina_com_coordendas.php", function (data) {
      // codigo para atualizar o JS, exemplo:
      // se o JSON que retornar do PHP for '{coordenadas: [x, y]}'
      var testMarker = L.marker(data.coordenadas, {icon: orangeIcon}).bindPopup('Kedson<br>Visto: 12:00pm'); 
  });
}, 5000);
  • 1

    I believe it is the setInterval, setTimeout will run only once, and it needs to be updated every 5 seconds

  • In case the page updates itself ?

  • I think that’s what he wants, that the value is updated every 5 seconds

  • @Antonio is right. I will update the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.