Skip to content

Instantly share code, notes, and snippets.

@tskrynnyk
Created November 8, 2011 17:51
Show Gist options
  • Select an option

  • Save tskrynnyk/1348518 to your computer and use it in GitHub Desktop.

Select an option

Save tskrynnyk/1348518 to your computer and use it in GitHub Desktop.
$(document).ready(function() {
var fortune_script = '/bofh-excuse.php';
var rotate_timeout = 10000;
var spinner_timeout = 2000;
var rgb = { min:40, range:100 };
var rotate_count = { all:0, max:500, errors:0, max_errors:3 }; /* max:0 for unlimited */
var spinner = $("#spinner");
var fortune = $("#fortune");
var ajax_timeout = 20000;
var ajax_error_msg = "Oops! Error 0xFEE1DEAD.";
function rotateExcuse() {
showSpinner = setTimeout(function() { spinner.addClass("loading").fadeIn(); }, spinner_timeout);
var startTime = new Date().getTime();
$.ajax({
url: fortune_script + '?' + startTime,
timeout: ajax_timeout,
dataType: "text",
cache: false,
success: function (data) {
fortune.fadeOut('normal', function() {
$(this).css('color', 'rgb(' + [
(Math.floor((Math.random() * rgb.range) + rgb.min)),
(Math.floor((Math.random() * rgb.range) + rgb.min)),
(Math.floor((Math.random() * rgb.range) + rgb.min))
].join(",") + ')');
$(this).html(data).fadeIn('normal', function() {
if (rotate_count.max == 0 || ++rotate_count.all < rotate_count.max) {
showExcuse = setTimeout(rotateExcuse, rotate_timeout);
} else {
rotate_count.all = 0;
setTimeout(function() { fortune.trigger("click"); }, rotate_timeout);
}
//console.log(rotate_count.all + '/' + rotate_count.max);
});
});
},
error: function(xhr, ajaxOptions, thrownError) {
if (++rotate_count.errors < rotate_count.max_errors) {
showExcuse = setTimeout(rotateExcuse, rotate_timeout);
} else {
fortune.fadeOut('normal', function() {
$(this).html(ajax_error_msg).fadeIn('normal');
});
}
//console.log(rotate_count.errors + '/' + rotate_count.max_errors + ': ' + xhr.statusText);
},
complete: function () {
clearTimeout(showSpinner);
spinner.fadeOut('normal', function() { $(this).removeClass("loading"); });
//console.log((new Date().getTime() - startTime) + 'ms');
}
});
}
function fortuneBlink() {
//fortune.animate({ opacity: 'toggle' });
fortune
.animate({ opacity: 0 }, 400, "swing")
.animate({ opacity: 0.8 }, 400, "swing");
}
var fortunePaused;
//setTimeout(rotateExcuse, 5000);
setTimeout(function() {
rotateExcuse();
fortune.toggle(function() {
//clearTimeout(showExcuse);
//fortunePaused = setInterval(fortuneBlink, 1000);
$(this).fadeTo("normal", "0.4", function () { clearTimeout(showExcuse); spinner.addClass("paused").html("[paused]").fadeIn(); });
}, function() {
//clearInterval(fortunePaused);
//$(this).animate({ opacity: 1.0 }, 400, "swing");
//setTimeout(rotateExcuse, rotate_timeout);
$(this).fadeTo("normal", "1.0", function () { setTimeout(rotateExcuse, rotate_timeout); spinner.fadeOut('normal', function() { $(this).removeClass("paused").html(""); }) });
}
);
fortune.css('cursor', 'pointer');
}, 5000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment