jquery - Json update every x seconds SetInterval -
i made script json information:
$(document).ready(function(){ $.ajax({ url: 'url', datatype: 'json', cache: true, timeout: 30000, success: function(data) { // $('#output ul').append('<li>the feed loads fine'); $('#output ul').empty(); $.each(data.posts, function(i,data){ $('#output ul').append('<li><a href="'+data.image+'"><img class="thumb" src="'+data.image+'" alt="" /></a><h3>'+data.title+'</h3><p>'+data.text+'</p></li>'); }); }, error: function(){ $('#output ul').append('<li>error'); } }); });
but want update feed every x seconds. read lot can done.
how can this?
use setinterval repeated phone call code block, , set code in function , pass function name setinterval
first parameter. can pass anonymous function instead of making new function repeatme
prefer making function create code more readable.
function repeatme(){ $.ajax({ url: 'url', datatype: 'json', cache: true, timeout: 30000, success: function(data) { // $('#output ul').append('<li>the feed loads fine'); $('#output ul').empty(); $.each(data.posts, function(i,data){ $('#output ul').append('<li><a href="'+data.image+'"><img class="thumb" src="'+data.image+'" alt="" /></a><h3>'+data.title+'</h3><p>'+data.text+'</p></li>'); }); }, error: function(){ $('#output ul').append('<li>error'); } }); } setinterval(repeatme, 5000);
edit improve utilize settimeout instead of setinterval in success send next phone call update after first has finished job. set settimeout in error maintain repetive phone call update.
function repeatme(){ $.ajax({ url: 'url', datatype: 'json', cache: true, timeout: 30000, success: function(data) { // $('#output ul').append('<li>the feed loads fine'); $('#output ul').empty(); $.each(data.posts, function(i,data){ $('#output ul').append('<li><a href="'+data.image+'"><img class="thumb" src="'+data.image+'" alt="" /></a><h3>'+data.title+'</h3><p>'+data.text+'</p></li>'); settimeout(repeatme, 5000); }); }, error: function(){ $('#output ul').append('<li>error'); settimeout(repeatme, 5000); } }); }
jquery json
No comments:
Post a Comment