Skip to content

Instantly share code, notes, and snippets.

@mikesjewett
Created October 16, 2012 17:52
Show Gist options
  • Save mikesjewett/3900867 to your computer and use it in GitHub Desktop.
Save mikesjewett/3900867 to your computer and use it in GitHub Desktop.
Simple Javascript Timer
<div class="row">
<div class="span6">
<% if @meeting.started && [email protected]? %>
<%= content_for :javascripts do %>
<script>
var count = <%= @meeting.before_time %>;
var cost = <%= @meeting.cost %>;
</script>
<% end %>
<div class="live_input">
<%= form_for(@meeting) do |f| %>
<label for="meeting_cost">Cost</label>
<input id="meeting_cost" name="Cost" readonly="true" value="<%= @meeting.cost %>">
<%= f.label :time %>
<%= f.text_field :before_time, readonly: true %>
<%= f.submit "Stop meeting", class: "btn btn-large btn-primary" %>
<% end %>
</div>
<% else %>
<div>
<p>
<% if @meeting.completed? %>
<%= "This meeting was
#{ pluralize(@meeting.after_time, 'minute') } long and cost
#{ pluralize(number_to_currency(@meeting.cost, precision: 0), 'dollar') }."
%>
<% else %>
<%= "This meeting is expected to be
#{ pluralize(@meeting.before_time, 'minute') } long and expected to cost
#{ pluralize(number_to_currency(@meeting.cost, precision: 0), 'dollar') }."
%>
<% end%>
</p>
</div>
<% end %>
</div>
</div>
$(function() {
if (($("#meeting_before_time").length > 0) && ($("#meeting_cost").length > 0)) {
var clock=self.setInterval(function(){timer()}, 1000);
var seconds = 60;
count = count-1
function timer() {
seconds = seconds-1;
if (seconds < 1) {
seconds = 60;
count=count-1;
if (count <= 1) {
clearInterval(clock);
$("#meeting_before_time").val("END")
return;
}
}
if (seconds < 10) {
$("#meeting_before_time").val(count+
" : " + "0" + seconds);
} else {
$("#meeting_before_time").val(parseFloat(count).toFixed(0) +
" : " + parseFloat(seconds).toFixed(0));
}
}
}
});
$(function() {
if (($("#meeting_before_time").length > 0) && ($("#meeting_cost").length > 0)) {
var running_cost = self.setInterval(function(){meter()}, 1000);
var starting_cost = 0;
var cost_per_second = cost/(count*60);
function meter() {
starting_cost = starting_cost + cost_per_second
if (starting_cost >= cost) {
clearInterval(running_cost);
$("#meeting_cost").val("SPENT")
return;
}
$("#meeting_cost").val("$" + parseFloat(starting_cost).toFixed(2));
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment