JavaScript Two synced events creating an infinite loop

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();
Answer:1

A quick method of preventing an infinite propagation loop:

// Infinite loop prevention
var bounceProtection = {
  remoteUpdate: false, // remote toggle
  local: function(cb) {
    if (this.remoteUpdate) return;
    cb();
  },
  remote: function(cb) {
    this.remoteUpdate = true;
    cb();
    this.remoteUpdate = false;
  }
};

var myRoom = platform.room('myRoom');
var myKey = myRoom.key('myKey');

myKey.on('set', function(textAreaContent) {
  bounceProtection.local(function() {
    $('textarea').val(textAreaContent);
  });
});

$('textarea').on('change', function(){
  var textAreaContent = $(this).val();
  bounceProtection.remote(function() {
    myKey.set(textAreaContent, function(err) {
      if (err) throw err;
    });
  });
});
Answer:2

Simply remove the listener before editing the textarea and then reapply it (I also cached your textarea for you so you don't search the DOM on every triggered event).

I also suggest you give the textarea an ID attribute as it appears you are working with one textarea but doing a tag search which is inefficient and easily broken by adding another textarea to the page.

var myRoom = platform.room('myRoom');
var myKey = myRoom('myKey');

var $textarea = $('textarea');

function setKey() {
  var textAreaContent = $(this).val();
  myKey.set(textAreaContent, function(err) {
    if (err) throw err;
  });
}

// Listen to set events on the platform key and update a textarea
myKey.on('set', function(textAreaContent) {
  $textarea.off('change', setKey); // Remove listener
  $textarea.val(textAreaContent);
  $textarea.on('change', setKey); // Reapply listener
});

// When the textarea changes, set the platform key
$textarea.on('change', setKey);
Answer:3

This might have been asked before, but I didn't see exactly this question: Is there a way to declare an object in javascript such that I can directly assign values in multiple levels of nesting (...

This might have been asked before, but I didn't see exactly this question: Is there a way to declare an object in javascript such that I can directly assign values in multiple levels of nesting (...

  1. assign nested object javascript
  2. object assign nested object
  3. lodash assign nested object
  4. destructuring assignment nested object
  5. object assign nested properties
  6. object assign nested array
  7. object assign nested json

I want to change the color of each bar in my bar graph. Currently, I tried setting the colors option as specified in the documentation: var options = { 'title' : results.title, 'width'...

I want to change the color of each bar in my bar graph. Currently, I tried setting the colors option as specified in the documentation: var options = { 'title' : results.title, 'width'...

  1. google charts change font
  2. google charts change color
  3. google charts change color of bar
  4. google charts change background color
  5. google charts change legend text
  6. google charts change legend color
  7. google charts change font color
  8. google charts change line color
  9. google charts change tooltip text
  10. google charts change chart type
  11. google charts change font size
  12. google charts change data
  13. google charts change size
  14. google charts change legend position
  15. google charts change column color
  16. google charts change text color
  17. google charts change color based on value
  18. google charts change color dynamically
  19. google charts change width
  20. google charts change legend text color

My first application is running on 8080, the second application is running on 8081, The code is as follows: server { listen 80; server_name greatwallprojects.com; location / { ...

My first application is running on 8080, the second application is running on 8081, The code is as follows: server { listen 80; server_name greatwallprojects.com; location / { ...

I run the below JS script but just find func2() output is foobar, not George, who can explain why func2 = func.bind(someuser) doesn't bind someuser to func? var someuser = { name: 'George', ...

I run the below JS script but just find func2() output is foobar, not George, who can explain why func2 = func.bind(someuser) doesn't bind someuser to func? var someuser = { name: 'George', ...