arrays - jQuery ajax request to return data and repeat -
jquery code
var seconds = 6; var timeout = seconds * 1000; function request() { $.ajax ({ url: "getdata.php", datatype: "json" }).done(function(data) { console.log(data); // check if json data returned php script // somehow put returned json data variable or return it? // problem }); } function doloop() { request() // execute ajax request data database var items = // returned json data function request() var count = items.length; var repeat = count * timeout; $(items).each(function(idx, text) { $("#text").queue(function(next) { $(this).html(text); next(); }).delay(timeout); }); } setinterval(function() { doloop(); }, repeat);
the problem
the problem can't ajax call return json data can put array. results in not being able count how many items there in array , calculate interval looping procedure.
what wish accomplish repeating procedure, using setinterval, that:
- gets data database function
request
- put returned json data
request
arrayitems
- determine amount of items in array using
var count = items.length
- determine delay
setinterval
usingvar repeat = timeout * count
- process array displaying items within, 1 @ time (this part works)
- after processing entire array, return step 1 refresh data. (should no problem setinterval)
var seconds = 6; var timeout = seconds * 1000; var repeat; function doloop() { $.ajax ({ url: "getdata.php", datatype: "json" }).done(function(data) { var items = data; var count = items.length; repeat = count * timeout; $(items).each(function(idx, text) { $("#text").queue(function(next) { $(this).html(text); next(); }).delay(timeout); }); settimeout(doloop, repeat); // use settimeout() instead make sure //script won't trigger before finishing previous job. }); } $(document).ready(doloop);
Comments
Post a Comment