jquery setinterval

jquery setinterval

Hi i'm new to this post and i'm currently studying jquery. I have a jquery book and have been following it's examples to the T. but i've come across a problem when it talks about setinterval. The my coded example simply just doesn't work. I typed it word for word, letter for letter and it simply doesn't work. I've tried it in chrome and explorer, been trying to figure out a solution for some time now please help. Thanks.

Here is the js code:
  1. $(function(){
  2. var $green = $('#green');
  3. greenLeft = $green.offset().right;
  4. });
  5. $(function(){
  6. setInterval(function() {
  7. $green.css('left', ++greenLeft);
  8. }, 200);
  9. });

  10. $(function(){
  11. var $red = $('#red'),
  12. redLeft = $('#red').offset().left;
  13. function moveRed() {
  14. $red.css('left', ++redLeft);
  15. setTimeout(moveRed, 200);
  16. }
  17. moveRed();
  18. });
html code:
  1. <div id="green" class="box">Go!</div>
  2. <div id="red" class="box">Go!</div>
PS. All my plug-ins are there and correct because it works with other example