title: Sending data to Shiny server and back happens with delay
body: <p>I am expecting that by pressing the square div/button (3rd element on the page) a random number will appear under the element and the color of the element will also change. Unfortunately it seems that I have to press the button on the top of the page in order for this to happen and I can't figure out why?</p>
  
  <p>my <strong>index.html</strong></p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script type="text/javascript" src="shared/jquery.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript" src="shared/shiny.js"&gt;&lt;/script&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;input id="button" type="submit" name="submit" value="Press me!" class="action-button"&gt;
          &lt;p&gt;&lt;div id="text" class="shiny-text-output" style="width: 150px; height: 25px; border: 1px solid black;"&gt;&lt;/div&gt;
          &lt;p&gt;&lt;div id="mydiv" style="width: 50px; height: 50px;"&gt;&lt;/div&gt;
          &lt;pre id="results" class="shiny-text-output"&gt;&lt;/pre&gt;
      &lt;/body&gt;
  
      &lt;script type="text/javascript"&gt;
          // send data to shiny server
          document.getElementById ("mydiv").onclick = function() {
              var number = Math.random();
              Shiny.onInputChange ("mydata", number);
          };
  
          // handlers to receive data from server
          Shiny.addCustomMessageHandler ("myColorHandler",
              function (color) {
                  document.getElementById ("mydiv").style.backgroundColor = color;
              }
          );
      &lt;/script&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>my <strong>server.R</strong></p>
  
  <pre><code>shinyServer (function (input, output, session)
  {
      observe ({
          x = input$button;
  
          output$text = renderText ({
              if (! is.null (x))
              {
                  if (x %% 2 == 0) { paste (x, ": you are even", sep=""); }
                  else { paste (x, ": you are odd", sep=""); }
              }
          })
      });
  
      output$results = renderPrint ({
          input$mydata;
      })
  
      observe ({
          input$mydata;
          color = rgb (runif (1), runif (1), runif (1));
          session$sendCustomMessage (type="myColorHandler", color);
      });
  })
  </code></pre>