Skip to content

Commit 618bbf0

Browse files
committed
CHECK OUT DIS SESSIONS AND AJAX CALL STORING
1 parent 1b94a5b commit 618bbf0

File tree

8 files changed

+80
-49
lines changed

8 files changed

+80
-49
lines changed

app/assets/javascripts/button.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// we want this global timer to be accessed by all user threads
2+
3+
var userOwns = false;
4+
5+
$(document).on('click', '#button', function() {
6+
if(userOwns == false) {
7+
startClock();
8+
userOwns = true;
9+
}
10+
else {
11+
// when someone else clicks button, you lose it and get set to zero
12+
// store session name and time in db
13+
14+
var stopTime = $time.innerHTML;
15+
$.post("/users",
16+
{
17+
time: stopTime
18+
},
19+
function() {
20+
alert("congratz u got a time of: " + stopTime);
21+
}
22+
);
23+
stopClock();
24+
resetClock();
25+
userOwns = false;
26+
}
27+
});

app/assets/javascripts/clock.js

+38-42
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
1-
$(document).on('click', '#button', function() {
2-
start();
3-
});
4-
51
var clsStopwatch = function() {
62
// Private vars
7-
var startAt = 0; // Time of last start / resume. (0 if not running)
8-
var lapTime = 0; // Time on the clock when last stopped in milliseconds
3+
var startAt = 0; // Time of last start / resume. (0 if not running)
4+
var lapTime = 0; // Time on the clock when last stopped in milliseconds
5+
6+
var now = function() {
7+
return (new Date()).getTime();
8+
};
99

10-
var now = function() {
11-
return (new Date()).getTime();
12-
};
13-
14-
// Public methods
15-
// Start or resume
16-
this.start = function() {
17-
startAt = startAt ? startAt : now();
18-
};
10+
// Public methods
11+
// Start or resume
12+
this.startClock = function() {
13+
startAt = startAt ? startAt : now();
14+
};
1915

20-
// Stop or pause
21-
this.stop = function() {
22-
// If running, update elapsed time otherwise keep it
23-
lapTime = startAt ? lapTime + now() - startAt : lapTime;
24-
startAt = 0; // Paused
25-
};
16+
// Stop or pause
17+
this.stopClock = function() {
18+
// If running, update elapsed time otherwise keep it
19+
lapTime = startAt ? lapTime + now() - startAt : lapTime;
20+
startAt = 0; // Paused
21+
};
2622

27-
// Reset
28-
this.reset = function() {
29-
lapTime = startAt = 0;
30-
};
23+
// Reset
24+
this.resetClock = function() {
25+
lapTime = startAt = 0;
26+
};
3127

32-
// Duration
33-
this.time = function() {
34-
return lapTime + (startAt ? now() - startAt : 0);
35-
};
36-
};
28+
// Duration
29+
this.time = function() {
30+
return lapTime + (startAt ? now() - startAt : 0);
31+
};
32+
};
3733

3834
var x = new clsStopwatch();
3935
var $time;
@@ -59,27 +55,27 @@ function formatTime(time) {
5955
return newTime;
6056
}
6157

62-
function show() {
58+
function showClock() {
6359
$time = document.getElementById('time');
64-
update();
60+
updateClock();
6561
}
6662

67-
function update() {
63+
function updateClock() {
6864
$time.innerHTML = formatTime(x.time());
6965
}
7066

71-
function start() {
72-
clocktimer = setInterval("update()", 1);
73-
x.start();
67+
function startClock() {
68+
clocktimer = setInterval("updateClock()", 1);
69+
x.startClock();
7470
}
7571

76-
function stop() {
77-
x.stop();
72+
function stopClock() {
73+
x.stopClock();
7874
clearInterval(clocktimer);
7975
}
8076

81-
function reset() {
82-
stop();
83-
x.reset();
84-
update();
77+
function resetClock() {
78+
stopClock();
79+
x.resetClock();
80+
updateClock();
8581
}

app/controllers/users_controller.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
class UsersController < ApplicationController
22
def create
3-
User.create(user_params)
3+
User.create(
4+
name: session[:name],
5+
time: params[:time]
6+
)
7+
redirect_to root_path
8+
end
9+
10+
def store_user
11+
# so user can stay on same page and reuse session name
12+
session[:name] = user_params[:name]
413
redirect_to root_path
514
end
615

app/views/layouts/application.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
77
<%= csrf_meta_tags %>
88
</head>
9-
<body onload="show();">
9+
<body onload="showClock();">
1010

1111
<%= yield %>
1212

app/views/users/_new.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<h4 class="modal-title">Sign in</h4>
66
</div>
77
<div class="modal-body">
8-
<%= form_for @user, remote: true, url: {controller: "users", action: "create"}, class: "form-inline" do |f| %>
8+
<%= form_for @user, remote: true, url: store_user_path, method: "post", class: "form-inline" do |f| %>
99
<%= f.label "Hi! What's your name?" %>
1010
<%= f.text_field :name, class: "form-control" %>
1111
</div>

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Rails.application.routes.draw do
22
root 'home#index'
33
resources :users
4+
post 'store_user', to: 'users#store_user', as: 'store_user'
45
end

db/migrate/20160615013423_create_users.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ class CreateUsers < ActiveRecord::Migration
22
def change
33
create_table :users do |t|
44
t.string :name
5-
t.string :email
6-
t.integer :score
5+
t.string :time
76

87
t.timestamps null: false
98
end

db/schema.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
create_table "users", force: :cascade do |t|
1717
t.string "name"
18-
t.string "email"
19-
t.integer "score"
18+
t.string "time"
2019
t.datetime "created_at", null: false
2120
t.datetime "updated_at", null: false
2221
end

0 commit comments

Comments
 (0)