Randomly Display a List of Data with JavaScript Using Singleton Pattern

Just a small object that loops through data from an AJAX request and updates the diplay every 10 seconds with a new item from the list.

Item


var myList = (function()
{
var instance = null;

function init()
{
var myList = [];

function startLoading()
{
var num = Math.ceil(Math.random()*myList.length);

$('#item-title').fadeOut("slow", function(){
$('#item-title').html(myList[num].item);
$('#item-title').fadeIn("slow", function(){
$('#item-title');
});
});

setTimeout(function(){startLoading();}, "10000");
}

return {
start: function()
{
// Populate objects list.
$.ajax({
url: '/service/get-items-list',
}).done(function(data){
myList = JSON.parse(data);
startLoading();
});
}
};
};

return {

getInstance: function()
{
if( !instance )
instance = init();

return instance;
}
};
})();