basic jquery promise

by Eric Miller

HTML

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<span id="count">{count}</span> - <span id="color">{Color}</span>

JavaScript

var colors = ['red', 'blue', 'green'],
  color = colors[0],
  x = 0;

function changeColor() {
  color = colors[Math.floor(Math.random() * colors.length)];
  return;
}

$(function() {
  setInterval(function() {
    console.log(x, color);
    $('#color').css('color', color).text(color).promise().done(function() {
      changeColor();
      $('#count').text(x);
      x++;
    });
  }, 1000);
});