Jump to content

Can someone please show me how to make this more efficient?


xcandiottix

Recommended Posts

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.