Skip to content
This repository was archived by the owner on Jun 30, 2024. It is now read-only.

Commit 897a655

Browse files
committed
New: new technique for “waiting for a variable”
This is better than just using a timer and guessing how long it will take for some variables to be ready.
1 parent 4b986c4 commit 897a655

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

views/peer/peer_question.html

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,45 +40,69 @@ <h2>Peer Instruction: {{=assignment_name}}</h2>
4040
<form>
4141
<div class="form-group">
4242
Please provide a confidential rating of <select id="peersel" name="peercel"></select>
43-
peer's explanation<br />
44-
<input class="form-check-input ratingradio" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="poor"
45-
onclick="ratePeer(this)">
43+
peer's explanation<br />
44+
<input class="form-check-input ratingradio" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="poor" onclick="ratePeer(this)">
4645
<label class="form-check-label" for="inlineRadio1">Poor</label>
47-
<input class="form-check-input ratingradio" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="good"
48-
onclick="ratePeer(this)">
46+
<input class="form-check-input ratingradio" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="good" onclick="ratePeer(this)">
4947
<label class="form-check-label" for="inlineRadio2">Good</label>
50-
<input class="form-check-input ratingradio" type="radio" name="inlineRadioOptions" id="inlineRadio3"
51-
value="excellent" onclick="ratePeer(this)">
48+
<input class="form-check-input ratingradio" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="excellent" onclick="ratePeer(this)">
5249
<label class="form-check-label" for="inlineRadio3">Excellent</label>
5350
</div>
5451
</form>
5552
</div>
5653
</div>
57-
<p>Please do not leave or refresh the page during a peer instruction session. <strong>Exception:</strong> If you should have seen a new question at this point then you may continue.</p>
54+
<p>Please do not leave or refresh the page during a peer instruction session. <strong>Exception:</strong> If you should have seen a new question at this point then you may continue.</p>
5855

5956
<script src="/runestone/static/js/peer.js?v={{=request.peer_mtime}}"></script>
6057
<script>
6158
var user = '{{=auth.user.username}}';
6259
var currentQuestion = '{{=XML(current_question.name)}}';
6360
var vote2done = false;
64-
document.addEventListener("DOMContentLoaded", function (event) {
61+
document.addEventListener("DOMContentLoaded", function(event) {
6562
setTimeout(connect, 1000);
66-
setTimeout(function () {
67-
window.mcList[currentQuestion].submitButton.innerHTML = "Submit";
68-
window.mcList[currentQuestion].submitButton.addEventListener(
69-
"click",
70-
function() {
71-
if (studentVoteCount == 2) {
72-
vote2done = true;
73-
}
63+
});
64+
65+
// This function serves the purpose of making sure that a variable is defined before we
66+
// access it.
67+
function ensureMcList(timeout) {
68+
var start = Date.now();
69+
return new Promise(waitForIt); // set the promise object
70+
// waitForIt makes the decision whether the condition is met
71+
// or not met or the timeout has been exceeded which means
72+
// this promise will be rejected
73+
// TODO -- this could/should be generalized into a library so it can
74+
// take the name of any variable as a string along with a function to do whatever needs to be done.
75+
function waitForIt(resolve, reject) {
76+
if (window.mcList && Object.keys(window.mcList).length > 0) {
77+
console.log("mcList is set, resolving...")
78+
resolve(window.mcList[currentQuestion]);
79+
}
80+
else if (timeout && (Date.now() - start) >= timeout)
81+
reject(new Error("timeout"));
82+
else {
83+
console.log("wiating for mclist")
84+
setTimeout(waitForIt.bind(this, resolve, reject), 30);
85+
}
86+
}
87+
}
88+
89+
// This says what to do when the mcList variable from above is defined.
90+
ensureMcList(50000).then(function() {
91+
console.log("mcList is set");
92+
window.mcList[currentQuestion].submitButton.innerHTML = "Submit";
93+
window.mcList[currentQuestion].submitButton.addEventListener(
94+
"click",
95+
function() {
96+
if (studentVoteCount == 2) {
97+
vote2done = true;
7498
}
75-
);
76-
}, 1000);
99+
})
77100
});
101+
78102
var studentVoteCount = 1;
79103
$(window).on(
80104
"beforeunload",
81-
function (event) {
105+
function(event) {
82106
// this actual value gets ignored by newer browsers
83107
if (this.done) {
84108
return;

0 commit comments

Comments
 (0)