Skip to content

Instantly share code, notes, and snippets.

@cleuton
Created May 9, 2014 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cleuton/7b4c14262ce84fc055c3 to your computer and use it in GitHub Desktop.
Save cleuton/7b4c14262ce84fc055c3 to your computer and use it in GitHub Desktop.
Demo jQuery
/*
* Script de eventos da página index.html
*/
var chave = null;
var calculando = false;
var original = '';
$(document).ready(function(){
$("#btn").click(function(){
if (!calculando) {
$.get("/operacao",
function(response,statusTxt,xhr) {
if (xhr.status == 200) {
chave = response.chave;
original = $("#btn").val();
$("#btn").val('Verificar');
calculando = true;
}
else {
alert("Erro: " + statusTxt);
}
}
);
}
else {
$("#saida").text('Verificando...');
$.get("/operacao/" + chave,
function(response,statusTxt,xhr) {
if (xhr.status == 200) {
if (response.status == 3) {
$("#saida").text(response.valor);
chave = null;
$("#btn").val(original);
calculando = false;
}
else if (response.status == 2) {
$("#saida").text('Ainda não está pronto... Tente mais tarde.');
calculando = true;
}
else if (response.status == 1) {
$("#saida").text('Não tem pedido de cálculo no servidor. Pode ter expirado.');
$("#btn").val(original);
calculando = false;
}
}
else {
alert("Erro: " + statusTxt);
}
}
);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment