Jump to content

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.