javascript - Parsing JSON objects in an array using jQuery -
i'm trying print out value of "title" each object, delay before next title replaces current one. json:
{"articles":[ "author":"author1", "title":"title1", "description":"description1" }, { "author":"author2", "title":"title2", "description":"description2" } ] }
and code
$(function getnews(){ $.getjson( "https://newsapi.org/v1/articles?source=google-news&sortby=top&apikey=",function out(json){ shownews(json) }); function shownews(json){ arr =[]; $.each(json.articles, function(index, value) { arr.push(value.title); }); for(k =0; k< arr.length; k++){ console.log(arr[k]); } settimeout(function(){getnews()},4000);}});
what i'm getting for-loop running once, , printing values of array @ same time. appreciated.
let array = [ { "author": "author1", "title": "title1", "description": "description1" }, { "author": "author2", "title": "title2", "description": "description2" } ]; (let index in array) { settimeout(function () { console.log(array[index]) }, 1000 * index) }
this example of using timeout.
it console out each item every second.
you can modify want whatever.
Comments
Post a Comment