
Originariamente inviata da
sixdas
Come posso convertirla nel tuo metodo?
JavaScript:
codice:
var dataUpdater = new (function () {
function sendRequest () {
oReq.open("GET", "http://sito.it/query.php", true);
oReq.send();
}
function reqListen () {
$("#result-chat").html(this.responseText);
$("#result-chat").fadeIn(1500);
console.log("Counter \"ShowMessages\"");
nSession = setTimeout(sendRequest, 5000);
}
function reqError (oErr) {
console.log("Chiamata ajax fallita!");
}
var
nSession = -1, oReq = new XMLHttpRequest();
oReq.onload = reqListen;
oReq.onerror = reqError;
this.start = function () {
if (nSession !== -1) { return false; }
sendRequest();
return true;
};
this.stop = function () {
clearTimeout(nSession);
nSession = -1;
};
})();
// Se non ti serve l'onload rimuovilo...
onload = dataUpdater.start;
HTML:
codice:
<span onclick="dataUpdater.stop();" style="color: #0000ff; text-decoration: underline; cursor: pointer;">Ferma il ciclo</span> |
<span onclick="dataUpdater.start();" style="color: #0000ff; text-decoration: underline; cursor: pointer;">Avvia il ciclo</span>