如何在每次更新时调用函数?以下是我的尝试。它不起作用。当我单独执行它时,函数本身工作正常。
new Ajax.PeriodicalUpdater('chatMessages', 'chat/receive_chat',
{
method: 'post',
parameters: {lId: latestId},
insertion: Insertion.Bottom,
frequency: 2,
decay: 2,
Onsuccess: val = function(){$('chatMessages').scrollTop = $('chatMessages').scrollHeight;}
});
答案 0 :(得分:2)
以下是一个工作示例:JSFiddle Example
var msgCount = 0;
new Ajax.PeriodicalUpdater('chatMessages', '/echo/html/', {
insertion: Insertion.Bottom,
frequency: 2,
decay: 2,
onSuccess: function() {
msgCount++;
var response = '<div class="msg">Message '+msgCount+'</div>';
response += '<div>'+bigLongString+'</div>';
$('chatMessages').innerHTML += response;
$('chatMessages').scrollTop = $('chatMessages').scrollHeight;
}
});
var bigLongString = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
答案 1 :(得分:1)
您有onsuccess
的作业。您需要的是对函数或内联函数的引用。
onsuccess: function(){$('chatMessages').scrollTop = $('chatMessages').scrollHeight;}
// or
onsuccess: my_callback_func
答案 2 :(得分:1)
这是否与区分大小写一样简单?
你有:
Onsuccess: function(){...}
应该是:
onSuccess: function(){...}