erlangcometnitrogen

what is best way to show timer on page


I wanted to know is this really overhead to use comet to update timer over using javascript at client site and posting to server on specific time interval to log time if required?

body() ->
 wf:comet(fun() -> counter(1) end),
 #panel { id=placeholder }.

counter(Count) ->
 timer:sleep(1000),
 wf:update(placeholder, integer_to_list(Count)),
 wf:flush(),
 counter(Count + 1).

I understand its overhead but I want to know how much? as all event communication happens over websocket..


Solution

  • Doing this with comet/websockets adds unnecessary overhead: an additional process on the server-side as well as additional bandwidth usage. If this was a continual status update of something happening server-side, that would be different, but merely incrementing a counter to display client-side is something best left client-side.

    Doing it client-side with javascript's setInterval easily eliminates any problems. Something like the following should get the job done.

    body() ->
      [
        #panel { id=placeholder },
        <<"
        <script>
          var my_counter = 1;
          function update_counter() {
            objs('placeholder').html(my_counter);
            my_counter++;
          }
          setInterval(update_counter, 1000);
         </script>
         ">>
      ].