xcandiottix Posted May 17, 2011 Share Posted May 17, 2011 I have a jquery / json script I wrote but I'm not very good at this yet. Can anyone help me make this cleaner? <html> <script type="text/javascript" src="../jquery/jquery-1.4.4.min.js"></script> <script type="text/javascript"> var items = []; var x = 0; $.getJSON('updates.js', function(data) { $.each(data, function(key, val) { items.push('<id="' + key + '">' + val + '</li>'); x++; }); }); var y = 0; var auto_refresh = setInterval( function(){ $('#sometext').fadeOut('slow', function(){ if(y < x){ $("#sometext").html(items[y]); y++; } else{ y = 0; $("#sometext").html(items[y]); } } ); $('#sometext').fadeIn('slow'); }, 2000); </script> <div id="sometext"> Hello </div> </html> The json contains: { "one": "Singular sensation", "two": "Beady little eyes", "three": "Little birds pitch by my doorstep" } I want the code to create an array from any key/value pairs in the json, count them, then for each fade it in then out of the DIV .. then replace with the next one on the the fade in. Once it has shown all of them, I;d like for it to restart. This code works right now but it seems awfully clunky to me. As a bonus, I'd like it if I could start at a random number. For example if I have 10 items in the array, it would be great to randomly start at 0 - 9 each page load. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/236602-can-someone-please-show-me-how-to-make-this-more-efficient/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.