<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1556</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1556" rel="self"/>
  <title>ravinggenius</title>
  <updated>Sat Jul 25 23:16:09 -0700 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor221904</id>
    <published>2009-07-25T23:16:09-07:00</published>
    <title>[JavaScript] On Countdown/countup timer</title>
    <content type="html">&lt;p&gt;I have found the problem; the rg.chrome object members couldn't be set until jQuery was loaded, and they were getting null references. Anyway it does count down, but it still hangs for a moment and them skips displaying a second or two. How can I make this faster or more efficient? I think the problem is in rg.app.updateScreen(). Any takers?&lt;/p&gt;

&lt;pre&gt;$(document).bind('ready', function () {
  rg.app.init();
});

var rg = {
  config: {
    counter: 0,
    countUp: false,
    interval: 1000,
    target: 0
  },

  chrome: {},

  helpers: {
    two: function (n) {
      return ((n &amp;gt; 9) ? '' : '0') + n;
    },

    three: function (n) {
      return ((n &amp;gt; 99) ? '' : '0') + ((n &amp;gt; 9) ? '' : '0') + n;
    }
  },

  app: {
    init: function () {
      rg.chrome.answer = $('#answer');
      rg.chrome.comment = $('#comment');

      rg.chrome.hour = $('#timer .hour');
      rg.chrome.minute = $('#timer .minute');
      rg.chrome.second = $('#timer .second');

      setInterval('rg.app.tick()', 1000);
    },

    sync: function () {
      $.getJSON('counts.json', function (data) {
        rg.chrome.answer.text(data.answer);
        rg.chrome.comment.text(data.comment);

        var c = data.countdown;
        rg.config.counter = (((c.hour * 60) + c.minute) * 60) + c.second;
      });
    },

    tick: function () {
      if (rg.config.counter === rg.config.target) {
        rg.app.sync();
      }

      if (rg.config.countUp) {
        rg.config.counter = rg.config.counter + 1;
      } else {
        rg.config.counter = rg.config.counter - 1;
      }

      rg.app.updateScreen();
    },

    updateScreen: function () {
      var offset = rg.config.counter;

      offset = offset / 60 / 60;
      rg.chrome.hour.text(rg.helpers.three(Math.floor(offset)));

      offset = (offset - Math.floor(offset)) * 60;
      rg.chrome.minute.text(rg.helpers.two(Math.floor(offset)));

      offset = (offset - Math.floor(offset)) * 60;
      rg.chrome.second.text(rg.helpers.two(Math.floor(offset)));
    }
  }
};&lt;/pre&gt;</content>
    <author>
      <name>ravinggenius</name>
      <email>rg+code@ravinggenius.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/967-countdown-countup-timer/refactors/221904" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code967</id>
    <published>2009-07-25T17:04:41-07:00</published>
    <updated>2009-07-25T23:16:11-07:00</updated>
    <title>[JavaScript] Countdown/countup timer</title>
    <content type="html">&lt;p&gt;I recently read the book JavaScript: The Good Parts. After reading the book, my existing JS wasn't very pretty. Since it was horribly slow, I decided to refactored to remove the &amp;quot;class&amp;quot; and all but one global variable. However now the countdown doesn't work at all. Everything looks correct to me; what did I do wrong?&lt;/p&gt;

&lt;p&gt;By the way, the project it now on GitHub (&lt;a href="http://github.com/ravinggenius/weekend/tree/master" target="_blank"&gt;http://github.com/ravinggenius/weekend/tree/master&lt;/a&gt;). Also I am using jQuery and Google's ajax api loader.&lt;/p&gt;

&lt;pre&gt;google.setOnLoadCallback(function () {
  rg.app.init();
});

var rg = {
  counter: 0,
  countUp: true,
  target: 0,

  chrome: {
    hour: $('#countdown .hour'),
    minute: $('#countdown .minute'),
    second: $('#countdown .second'),

    answer: $('#answer'),
    comment: $('#comment')
  },

  helpers: {
    two: function (n) {
      return ((n &amp;gt; 9) ? '' : '0') + n;
    },

    three: function (n) {
      return ((n &amp;gt; 99) ? '' : '0') + ((n &amp;gt; 9) ? '' : '0') + n;
    }
  },

  app: {
    init: function () {
      setInterval(rg.app.tick, 1000);
    },

    sync: function () {
      $.getJSON('counts.json', function (data) {
        rg.chrome.answer.text(data.answer);
        rg.chrome.comment.text(data.comment);

        var c = data.countdown;
        rg.counter = (((c.hour * 60) + c.minute) * 60) + c.second;
      });
    },

    tick: function () {
      if (rg.counter === rg.target) {
        rg.app.sync();
      }

      if (rg.countUp) {
        rg.counter = rg.counter + 1;
      } else {
        rg.counter = rg.counter - 1;
      }

      rg.app.updateScreen();
    },

    updateScreen: function () {
      var offset = rg.counter;

      offset = offset / 60 / 60;
      rg.chrome.hour.text(rg.helpers.three(Math.floor(offset)));

      offset = (offset - Math.floor(offset)) * 60;
      rg.chrome.minute.text(rg.helpers.two(Math.floor(offset)));

      offset = (offset - Math.floor(offset)) * 60;
      rg.chrome.second.text(rg.helpers.two(Math.floor(offset)));
    }
  }
};&lt;/pre&gt;</content>
    <author>
      <name>ravinggenius</name>
      <email>rg+code@ravinggenius.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/967-countdown-countup-timer" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor166869</id>
    <published>2009-06-21T16:35:36-07:00</published>
    <title>[Ruby] On Ruby: Date and Time methods</title>
    <content type="html">&lt;p&gt;Thank you for your help, but I'd really like to get rid of _monday_ and _advance_. Those methods come with Rails, but I can't use them because Rails is overkill for the site I'm building. Is there any way to replace those without -stealing- borrowing their implementations from Rails?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>ravinggenius</name>
      <email>rg+code@ravinggenius.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/927-ruby-date-and-time-methods/refactors/166869" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor166196</id>
    <published>2009-06-20T15:07:27-07:00</published>
    <title>[Ruby] On Ruby: Date and Time methods</title>
    <content type="html">&lt;p&gt;Well it helps to do one's homework. advance doesn't seem to be available outside of Rails either.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>ravinggenius</name>
      <email>rg+code@ravinggenius.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/927-ruby-date-and-time-methods/refactors/166196" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor166175</id>
    <published>2009-06-20T15:01:08-07:00</published>
    <title>[Ruby] On Ruby: Date and Time methods</title>
    <content type="html">&lt;p&gt;Trying to removed code that isn't native to Ruby. Just have the call to monday on the last line left.&lt;/p&gt;

&lt;pre&gt;class Message
  def initialize
    right_now = Time.now

    case Date.today.wday
      when 0, 6 # sunday, saturday
        @is_weekend = true
      when 1 # monday
        @is_weekend = (right_now.hour &amp;lt; 8)
      when 5 # friday
        @is_weekend = (right_now.hour &amp;gt;= 17)
      else
        @is_weekend = false
    end

    a = @is_weekend ? { :weeks =&amp;gt; 1, :hours =&amp;gt; 8 } : { :days =&amp;gt; 4, :hours =&amp;gt; 17 }
    @next_event = right_now.monday.advance(a)
  end
end&lt;/pre&gt;</content>
    <author>
      <name>ravinggenius</name>
      <email>rg+code@ravinggenius.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/927-ruby-date-and-time-methods/refactors/166175" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code927</id>
    <published>2009-06-20T07:53:53-07:00</published>
    <updated>2009-06-22T02:39:35-07:00</updated>
    <title>[Ruby] Ruby: Date and Time methods</title>
    <content type="html">&lt;p&gt;I have a small Ruby on Rails application that I'm porting to Sinatra. I didn't know it when I wrote the following excerpt, but monday is a Rails method. Anyway the results I am looking for are as follows:&lt;/p&gt;

&lt;p&gt;If the current time is between Monday at 08:00 and Friday at 17:00 (the work week), @is_weekend should be false and @next_event should be the nearest Friday at 5:00 in the future.&lt;/p&gt;

&lt;p&gt;If the current time is between Friday at 17:00 and Monday at 8:00 (the weekend), @is_weekend should be true and @next_event should be the nearest Monday at 08:00 in the future.&lt;/p&gt;

&lt;p&gt;I am not terribly concerned with time zone adjustments at the moment, but I will be later on.&lt;/p&gt;

&lt;pre&gt;class Message
  def initialize
    right_now = Time.now

    case Date.today.wday
      when 0, 6 # sunday, saturday
        @is_weekend = true
      when 1 # monday
        @is_weekend = (right_now.hour &amp;lt; 8)
      when 5 # friday
        @is_weekend = (right_now.hour &amp;gt;= 17)
      else
        @is_weekend = false
    end

    @next_event = right_now.monday + (@is_weekend ? 1.week + 8.hours : 4.days + 17.hours)
  end
end&lt;/pre&gt;</content>
    <author>
      <name>ravinggenius</name>
      <email>rg+code@ravinggenius.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/927-ruby-date-and-time-methods" rel="alternate"/>
  </entry>
</feed>

