/
experiment.html
executable file
·169 lines (131 loc) · 4.4 KB
/
experiment.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html>
<head>
<title>Decision Making Experiment</title>
<!-- Load libraries -->
<script src="https://unpkg.com/jquery"></script>
<script src="../static/lib/jspsych-7.2.1/jspsych.js"></script>
<!-- Load jsPsych plug-ins -->
<script src="../static/lib/jspsych-7.2.1/plugins/plugin-survey-likert.js"></script>
<!-- Load CSS styles -->
<link href="../static/lib/jspsych-7.2.1/css/jspsych.css" rel="stylesheet" type="text/css"></link>
</head>
<body></body>
<script>
// Display alert message on back/refresh.
// https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload
function verify_unload(e){
e.preventDefault();
(e || window.event).returnValue = null;
incomplete_save(); // Saves incomplete data.
return null;
};
window.addEventListener("beforeunload", verify_unload);
// Define example survey.
var likert_page = {
type: jsPsychSurveyLikert,
questions: [{prompt: "Are you seeing something right now?", labels: ["Yes", "No"]}],
on_start: function(data) {
pass_message('message to pass to nivturk');
}
};
// Define global variables.
var low_quality = false;
// Initialize timeline.
var timeline = [likert_page];
// Initialize jsPsych.
var jsPsych = initJsPsych({
on_finish: function() {
// Remove requirement to verify redirect
window.removeEventListener("beforeunload", verify_unload);
// Add interactions to the data variable
var interaction_data = jsPsych.data.getInteractionData();
jsPsych.data.get().addToLast({interactions: interaction_data.json()});
// Display jsPsych data in viewport.
// jsPsych.data.displayData();
if (low_quality) {
// Save rejected dataset to disk.
redirect_reject("1005");
} else {
// Save complete dataset to disk.
redirect_success("{{workerId}}", "{{assignmentId}}", "{{hitId}}", "{{code_success}}");
}
}
});
// Execute timeline.
jsPsych.run(timeline);
</script>
<script>
// Pass message from jsPsych to NivTurk
function pass_message(msg) {
$.ajax({
url: "/experiment",
method: 'POST',
data: JSON.stringify(msg),
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
// do nothing on success
}).fail(function(error) {
console.log(error);
});
}
// Save an incomplete dataset.
function incomplete_save() {
$.ajax({
url: "/incomplete_save",
method: 'POST',
data: JSON.stringify(jsPsych.data.get().json()),
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
// do nothing
}).fail(function(error) {
// do nothing
});
}
// Successful completion of experiment: redirect with completion code.
function redirect_success(workerId, assignmentId, hitId, code_success) {
// Concatenate metadata into complete URL (returned on success).
var url = "/complete?completion_code=" + code_success;
$.ajax({
url: "/redirect_success",
method: 'POST',
data: JSON.stringify(jsPsych.data.get().json()),
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
window.location.replace(url);
}).fail(function(error) {
window.location.replace(url);
});
}
// Unsuccessful completion of experiment: redirect with decoy code.
function redirect_reject(error) {
// Concatenate metadata into complete URL (returned on reject).
var url = "/error/" + error;
$.ajax({
url: "/redirect_reject",
method: 'POST',
data: JSON.stringify(jsPsych.data.get().json()),
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
window.location.replace(url);
}).fail(function(error) {
window.location.replace(url);
});
}
// Unsuccessful completion of experiment: redirect to error page.
function redirect_error(error) {
// error is the error number to redirect to.
var url = "/error/" + error;
$.ajax({
url: "/redirect_error",
method: 'POST',
data: JSON.stringify(jsPsych.data.get().json()),
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
window.location.replace(url);
}).fail(function(error) {
window.location.replace(url);
});
}
</script>
</html>