1
How to improve this script?
The function copiaArea()
should take the first return of the array and send to the windows clipboard, detail: I am developing for IE, works exclusively in it this function. But the global var numero
, does not receive on time on the second call from listaNumeros()
, after the adicionarNúmero()
(generated by Trigger in the BD, for date processing and direct numbering sequence in mysql.).
The question is as follows, the contraindications of using the async:false
in ajax are relevant here?
Another detail, is that this only runs on local network server, does not depend on the internet.
/*
* Loader da numeração
* @author marcelo aymone
*/
$(document).ajaxStart(function() {
$('#loading').show();
});
$(document).ajaxStop(function() {
$('#loading').hide();
});
var ip;
var numero;
function inserirLinha(array) {
var linha = $("<tr>");
var col = "";
col += "<td>" + array.num + "</td>";
col += "<td>" + array.time + "</td>";
linha.append(col);
if (array.ip == ip) {
linha.toggleClass("info");
}
$("#numeros").append(linha);
}
function sysMsg(msg) {
$("#alertas").empty().append(msg);
}
function copiaArea(txt) {
if (window.clipboardData && clipboardData.setData) {
clipboardData.setData('text', txt);
}
}
function atualizaTabela(data) {
var msg = "";
if (data.status) {
msg = "<div class=\"alert alert-success text-center\">";
msg += "<h4>Sucesso!</h4>";
msg += "Seu número foi gerado com sucesso!";
msg += "</div>";
} else {
msg = "<div class=\"alert alert-error\">";
msg += "<h4>Sucesso!</h4>";
msg += "Ocorreu um erro e não foi possível gerar um número.";
msg += "</div>";
}
$("#numeros").empty();
listarNumeros();
sysMsg(msg);
}
/*
* ListarNumeros recebe o seguinte json:
* {"lista":[{"num":"2014\/1618","ip":"10.120.2.35","time":"15\/05\/2014"}],"cliente":"10.120.2.35"}
**Possui \/ por causa do json_encode(), mas funciona ok.
*/
function listarNumeros() {
$.ajaxSetup({cache: false, async:false});
$.getJSON("/intracake/Numeros/listar.json")
.done(function(data) {
ip = data.cliente;
numero = data.lista[0].num; //Pega o 1 resultado do array, último num gerado.
$.each(data.lista, function(index, array) {
inserirLinha(array);
});
});
}
/*
* Recebe como retorno do json: {"status":true}
*/
function adicionarNumero() {
$.post("/intracake/Numeros/adicionar.json")
.done(function(data) {
atualizaTabela(data);
copiaArea(numero);
});
}
$(document).ready(function() {
listarNumeros();
});
Html+php script (cakephp):
<?php
echo $this->Html->script('numeros-run', array('inline' => false));
?>
<style>
.centralizado {
margin: 0 auto !important;
float: none !important;
}
.table td, .table th {
text-align: center;
}
</style>
<div class="span10">
<div id="alertas"></div>
<p class="text-center"><button class="btn btn-large btn-primary" type="button" onclick="adicionarNumero();">Gerar número!</button></p>
<div id="loading" class="text-center"><p><img src="img/ajax-loader.gif"></p></div>
<p class="text-center lead">Últimos números gerados:</p>
<div class="span4 centralizado">
<table id="numeros" class="table table-bordered">
</table>
</div>
<p class="text-center">
<i class="icon-info-sign"></i>
Todos números gerados pelo seu ip,
aparecem destacados em azul.
</p>
<p class="text-center">
<i class="icon-info-sign"></i>
Ao gerar um número, ele é enviado para a área de transferência do windows.<br>
<i class="icon-info-sign"></i>
Compatível apenas com Microsoft Internet Explorer.<br>
<strong class="text-error">Você precisa aceitar na janela que abrirá</strong>, para que o número possa ser copiado automaticamente.<br>
Depois basta você colar("ctrl+v") em qualquer lugar.
</p>
</div>
Yes, it can stop everything. What you need is a synchronization scheme (created by you, not by jQuery) using a global variable, for example
var estado = 0;
When you call one of the two functions, add 1 to the variable, and call a third functionexecutaFim()
. WithinexecutaFim()
, you validate ifestado
is worth 2. If yes, execute your code that depends on the two other functions have performed. Otherwise, do nothing.– carlosrafaelgn
@carlosrafaelgn, isn’t that the same? because if you put a condition that allows you to not perform anything, it is the same result of ajax having no return and keep the page static.
– Marcelo Aymone
No, it’s not the same, no. Because, despite "doing nothing", at least it didn’t catch the user screen, while the answer doesn’t arrive ;)
– carlosrafaelgn
I get it... but I don’t want to do this so that it looks like a gambiarra, I’d like to set in the callback’s running stack pattern.
– Marcelo Aymone
If I add the callback
.fail({return false;})
as callback of the ajax request itself, will not "unlock" the page and continue the execution?– Marcelo Aymone
That’s not "gambiarra". That’s kind of state machine very simple.
– carlosrafaelgn
Where the function is called
adicionarNumero
?– bfavaretto
"Crash" is not due to error or success, but rather due to the delay of the data reaching the customer’s machine (be it correct or wrong data)
– carlosrafaelgn
I get it, I’ll read the link... and try to understand how to implement, thanks @carlosrafaelgn
– Marcelo Aymone
@bfavaretto: No html, single button onclick.
<button class="btn btn-large btn-primary" type="button" onclick="adicionarNumero();">Gerar número!</button>
– Marcelo Aymone
The link is just to clarify that is not gambiarra do what I said (use the variable
estado
). You don’t need to read it all just to implement this suggestion, in case you’re in a hurry ;)– carlosrafaelgn
Yeah, I get it, I’m not in a hurry, I’m in it for the learning! Actually I take care of the infra... but you know how it is right... we always search the neighbor’s grass.
– Marcelo Aymone
@bfavaretto added the HTML, repairs not the lot of code, it is because using cakephp, twitter bootstrap and the requests in ajax format with jquery.
– Marcelo Aymone
@carlosrafaelgn I just didn’t understand the following,
if (estado ==1)
after the execution of the script, even though it still doesn’t work, it is running in afor
to realize thatestado
was changed? How to establish this "Systener" to verify thatestado
was changed? I tried to find some example on the net.. but only very complex scripts, being part of long applications.– Marcelo Aymone
I thought I would use something like this: http://www.douglaspasqua.com/2013/08/14/javascript-e-funcoes-de-retorno-callback/
– Marcelo Aymone
Try to see if the answer given was what you wanted, or take a look at this fiddle I just created (lines marked with
@@@
are the ones I created): http://jsfiddle.net/USnmL/1/– carlosrafaelgn