I have a text area that I am synchronizing with GoInstant. here is what the code looks like:
var myRoom = platform.room('myRoom');
var myKey = myRoom('myKey');
// Listen to set events on the platform key and update a textarea
myKey.on('set', function(textAreaContent) {
$('textarea').val(textAreaContent);
});
// When the textarea changes, set the platform key
$('textarea').on('change', function(){
var textAreaContent = $(this).val();
myKey.set(textAreaContent, function(err) {
if (err) throw err;
});
})
This creates an infinite loop, when updating one text field i.e. When changing the value of the textarea, this triggers a Platform key update, which in turn changes the value of the textarea infinitely ...
EDIT: Based on the top answer I came up with the following constructor:
function BounceProtection() {
var remoteUpdate = false; // remote toggle
this.local = function(cb) {
if (remoteUpdate) return;
cb();
};
this.remote = function(cb) {
remoteUpdate = true;
cb();
remoteUpdate = false;
};
}
This way, I can generate bounceProtection objects as needed to protect multiple keys even with the asynchronous nature of js.
var myKeyBP = new BounceProtection();