我正在使用原型js做一个定期更新程序,我想根据ajax请求发送的响应隐藏具有class='robot'
的页面的每个元素。如果响应等于hidethemall
,我希望隐藏每个class='robot'
,但如果不是,那么我们什么也不做,不执行更新
到目前为止,我能够在经过多次定期更新后重置功能,以免加载服务器。到目前为止,这是我的函数的样子
function mettreajour_periodique(span_id, url_traitement, nos_parametres,
our_frequency, our_decay, our_decay_to_reset)
{
var ajax = new Ajax.PeriodicalUpdater({success: span_id}, url_traitement,
{
method:'get',
frequency: our_frequency,
decay: our_decay,
parameters: nos_parametres,
evalScripts: true,
onSuccess: function(r) { //or onComplete
if (ajax.decay >= our_decay_to_reset)
{
ajax.decay = our_decay;
}
}
});
}
mettreajour_periodique('nombre_nouveaux_messages',
'messages_compter_non_lus_actualise.php', '&membre=lesucces', 10, 2, 1800);
问题
如果ajax响应等于class='robot'
,如何通过更新div而不是使用hidethemall
隐藏每个div来停止此操作?
答案 0 :(得分:1)
Ajax.PeriodicalUpdater
的问题是它使用Ajax.Updater
,它希望将响应放在某个地方而不是您想要做的事情。所以我会改为定义一个新类。
Ajax.PeriodicalHider = Class.create(Ajax.PeriodicalUpdater, {
initialize: function($super, selector, url, options) {
$super(null, url, options);
// selector are the elements to hide
this.selector = selector;
// max_decay is for extra time control
this.max_decay = this.options.max_decay;
},
onTimerEvent: function() {
// use a Request instead of an Updater
this.updater = new Ajax.Request(this.url, this.options);
},
updateComplete: function($super, response) {
$super(response);
if (this.max_decay) {
// not accurate, a new timer already has the wrong frequency
this.decay = Math.min(this.decay, this.max_decay);
}
if (response.responseText == 'hidethemall') {
$$(this.selector).each(Element.hide);
// stop now because there is no more to do
this.stop();
}
}
});
new Ajax.PeriodicalHider('.robot', 'messages_compter_non_lus_actualise.php', {
method: 'get',
frequency: 10,
decay: 2,
max_decay: 1800,
parameters: '&membre=lesucces'
});
这是tested。