Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
LEIA O POST 2 DEPOIS LEIA ESSE PARA ENTENDER O PROBLEMA
Boa noite galera.
Bom, eu estou fazendo manutenção num site quase todo em Mootools e precisava que uma DIV atualizasse a cada minuto.
Coisa simples de fazer mas sempre fiz com jQuery. Tentei colocar jquery junto mas dá incompatibilidade.
Até utilizei o
jQuery.noConflict(); antes do script do jquery e também:
jQuery(document).ready(function(){
//codigo aqui
});
O problema é que usando isso, a function roda ok mas quando ela termina (uso callback para chamar uma função), a função não acha a function inicial.. Mesmo se coloco a segunda dentro da primeira, não dá certo..
Então chutei o balde e tentei achar algo do Mootools que fizesse o mesmo.
(e assim, não precisaria carregar mais 55kb do jquery só para isso)
Achei mas achei para funcionar quando se tem link para clicar. Já procurei, vi que poderia mudar o addEvent('click' para 'load' mas mesmo assim nao acontece nada (lógico, mudando outras partes do codigo).
Gostaria de saber se alguém pode me ajudar com o código.. O código com link para clicar está aqui:
window.addEvent('domready', function() {
var url = 'http://demos111.mootools.net/demos/Ajax_Timed/ajax_timed.php';
// refresh every 4 seconds
var timer = 4;
// periodical and dummy variables for later use
var periodical, dummy;
var start = $('start'), stop = $('stop'), log = $('log_res');
/ our ajax istance /
var ajax = new Ajax(url, {
update: log,
method: 'get',
onComplete: function() {
// when complete, we remove the spinner
log.removeClass('ajax-loading');
},
onCancel: function() {
// when we stop timed ajax while it's requesting
// we forse to cancel the request, so here we
// just remove the spinner
log.removeClass('ajax-loading');
}
});
/* our refresh function: it sets a dummy to prevent
caching of php and add the loader class */
var refresh = (function() {
// dummy to prevent caching of php
dummy = $time() + $random(0, 100);
// we add out fancy spinner
log.empty().addClass('ajax-loading');
// requests of our php plus dummy as query
ajax.request(dummy);
});
// start and stop click events
start.addEvent('click', function(e) {
// prevent default
new Event(e).stop();
// prevent insane clicks to start numerous requests
$clear(periodical);
/* a bit of fancy styles */
stop.setStyle('font-weight', 'normal');
start.setStyle('font-weight', 'bold');
log.empty().addClass('ajax-loading');
/* ********************* */
// the periodical starts here, the * 1000 is because milliseconds required
periodical = refresh.periodical(timer * 1000, this);
// this is the first only request, later on will be only the periodical and refresh
// that do the request. If we don't do this way, we have to wait for 4 seconds before
// the first request.
ajax.request($time());
});
stop.addEvent('click', function(e) {
new Event(e).stop(); // prevent default;
/* a bit of fancy styles
note: we do not remove 'ajax-loading' class
because it is already done by 'onCancel'
since we later do 'ajax.cancel()'
*/
start.setStyle('font-weight', 'normal');
stop.setStyle('font-weight', 'bold');
/* ********************* */
// let's stop our timed ajax
$clear(periodical);
// and let's stop our request in case it was waiting for a response
ajax.cancel();
});
});<div id="form_box">
<ul>
<li><a id="start" href="#">start</a></li>
<li><a id="stop" href="#">stop</a></li>
</ul>
</div>
<div id="log">
<h3>Ajax Response</h3>
<div id="log_res"><!-- spanner --></div>
</div>
Como não vou precisar que apareça o loading nem nada, já tirei as partes não importantes aqui:
window.addEvent('domready', function() {
var url = 'http://demos111.mootools.net/demos/Ajax_Timed/ajax_timed.php';
// refresh every 4 seconds
var timer = 4;
// periodical and dummy variables for later use
var periodical, dummy;
var start = $('start'), stop = $('stop'), log = $('log_res');
/ our ajax istance /
var ajax = new Ajax(url, {
update: log,
method: 'get'
});
/* our refresh function: it sets a dummy to prevent
caching of php and add the loader class */
var refresh = (function() {
// dummy to prevent caching of php
dummy = $time() + $random(0, 100);
// we add out fancy spinner
log.empty().addClass('ajax-loading'); //ESSE NAO SEI SE DEIXO O log.empty(); OU TIRO TUDO
// requests of our php plus dummy as query
ajax.request(dummy);
});
// start and stop click events
start.addEvent('click', function(e) {
// prevent default
new Event(e).stop();
// prevent insane clicks to start numerous requests
$clear(periodical);
// the periodical starts here, the * 1000 is because milliseconds required
periodical = refresh.periodical(timer * 1000, this);
// this is the first only request, later on will be only the periodical and refresh
// that do the request. If we don't do this way, we have to wait for 4 seconds before
// the first request.
ajax.request($time());
});
stop.addEvent('click', function(e) {
new Event(e).stop(); // prevent default;
// let's stop our timed ajax
$clear(periodical);
// and let's stop our request in case it was waiting for a response
ajax.cancel();
});
});http://demos111.mootools.net/Ajax_Timed
Como fazer ele já iniciar ao carregar a página e não precisar ter stop?
Agradeço desde já, estou desde 13hs tentando fazer funcionar e já são 19hs e nada além de frustradas tentativas.
:(
Att.
RaphaelDDL
Carregando comentários...