gpt4 book ai didi

javascript - Prototype js 如何使定期更新器根据ajax响应隐藏类的元素

转载 作者:行者123 更新时间:2023-12-03 10:35:55 26 4
gpt4 key购买 nike

我正在使用原型(prototype)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响应等于hidethemall,如何阻止它更新div并使其隐藏带有类class='robot'的每个div?

最佳答案

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 .

关于javascript - Prototype js 如何使定期更新器根据ajax响应隐藏类的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28986717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com