Prototype JS Framework - Calling a function when updates occur with a periodic updater
How do I call a function on every update? Below is what I tried. It doesn't work. The function itself works fine when I execute it alone.
new Ajax.PeriodicalUpdater('chatMessages', 'chat/receive_chat',
{
method: 'post',
parameters: {lId: latestId},
insertion: Insertion.Bottom,
frequency: 2,
decay: 2,
Onsuccess: val = function(){$('chatMessag开发者_StackOverflow社区es').scrollTop = $('chatMessages').scrollHeight;}
});
Here is a working example: 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."
Is this as simple as a case-sensitivity issue?
You have:
Onsuccess: function(){...}
Should it be:
onSuccess: function(){...}
You have an assignment for onsuccess
. What you need is either a reference to a function or inline function.
onsuccess: function(){$('chatMessages').scrollTop = $('chatMessages').scrollHeight;}
// or
onsuccess: my_callback_func
精彩评论