我正在使用GoInstant开发一个应用程序,但键和通道之间的区别并不是很清楚.我什么时候应该使用键与频道?
我有一个文本区域,我正在与GoInstant同步.这是代码的样子:
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;
});
})
Run Code Online (Sandbox Code Playgroud)
这会创建一个无限循环,当更新一个文本字段时,即当更改textarea的值时,这会触发一个平台密钥更新,从而无限地更改textarea的值...
编辑:基于最佳答案我想出了以下构造函数:
function BounceProtection() {
var remoteUpdate = false; // remote toggle
this.local = function(cb) {
if (remoteUpdate) return;
cb();
};
this.remote = function(cb) {
remoteUpdate = …Run Code Online (Sandbox Code Playgroud)