2016-08-09 7 views
0

Wie kann ich verschiedene JSON-Dateien mit JavaScript lesen und laden? Beispiel: Json1, Json2, Json3, Json4 einige Schleife das ist Last auf verschiedenen Json alle 30sLaden und Lesen von verschiedenen JSON-Dateien mit MathRandom()

versuchte und auf der Suche, wie man es machen, aber nichts funktioniert für mich, eine Idee?

will in diesem Code

function loadpoints() { function getData(){ 

    $.getJSON('pointer.json', function(pointer) { 

     var latlngbounds = new google.maps.LatLngBounds(); 

     $.each(pointer, function(index, ponto) { 
     //where its my data working 

     var marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(ponto.lat, ponto.lon), 
      title: ponto.activity, 
      icon: ponto.activity + '.png', 
      map: map 
     }); 

     console.log("created marker " + marker.position) 

     }); 

     setTimeout (getData, 3000); }); 
     } 

    getData(); 

    } 
+0

Warum Sie den Körper von getData Funktion innerhalb einer anderen Funktion setzen? – Mojtaba

+0

wurde triyng, um die Funktion neu laden funktioniert. aber alredy Änderung –

+0

setzen Sie Ihren neuen Code – Mojtaba

Antwort

0
@Mojtaba 
function genereteRamdon (min, max){ 
    var arr=[]; 
    while (arr.length<4){ 
     var randomNumber = Math.floor(Math.random() * (max-min+!))+min; 
     found=false; 

     for (var i=0, i<arr.length; i++){ 
     if (arr[1] = randomNumber){found=true; break} 
     } 
     return arr; 
    } 

    } 



    $.getJSON('pointer.json', function(pointer) { 

    var latlngbounds = new google.maps.LatLngBounds(); 

    $.each(pointer, function(index, ponto) { 


     var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(ponto.lat, ponto.lon), 
     title: ponto.activity, 
     icon: ponto.activity + '.png', 
     map: map 
     }); 

     console.log("created marker " + marker.position) 

    } 
0

Es ist nicht viel klar ist, was Sie zu bekommen versuchen.

Ich denke, Sie dies wünschen:

function getData(){ 

    $.getJSON('pointer.json', function(pointer) { 

     var latlngbounds = new google.maps.LatLngBounds(); 

     $.each(pointer, function(index, ponto) { 

      var marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(ponto.lat, ponto.lon), 
      title: ponto.activity, 
      icon: ponto.activity + '.png', 
      map: map 
      }); 

      console.log("created marker " + marker.position) 

     }); 

} 

getData(); 
setInterval(getData, 30000); 
+0

Ich habe 3 externe Dateien 1.json, 2.json und 3.json innerhalb meines Javascript ich brauche/etwas versuchen mit math.random zu lesen, Länge zu berechnen Größe meines Arrays und etwas, um meine JSON-Datei wie $ .get aufrufen. können Sie mir bitte helfen? , um eine zufällige Lese (math.random) und dann Anzeige ich glaube, ich brauche etwas mit –

0

Lösung :)

dieses Stück Code suchte

var min=1; var max = 4 // max number off json 

    var random = Math.floor(Math.random()*max)+min; var jsonfile = 
    "pointer" + random.toString()+".json" //display one of the files 


    $.getJSON(jsonfile, function(pointer) 
Verwandte Themen