Experiment Client v2 (with done)
This shows how to connect to Zooniverse Experiment Server. The Experiments must be defined server side.
by alexbfree
HTML
<form>
<label for='username'>Enter your username:</label>
<input type='text' name='username' id='username' placeholder='e.g. johnsmith1234'></input>
<input id='getCohortButton' type="button" value="What's my cohort?" />
</form>
<div>
<h1 id="yourCohort">
</h1>
<p id="postPara"></p>
</div>
JavaScript
var experiment = "SerengetiAwarenessExperiment1";
var experimentSummary = "(Current cohorts (25% split each) = [control, both, self, crowd])"
var getCohort = function (user) {
var def = $.ajax({
url: 'http://experiments.zooniverse.org/experiment/' + experiment + '?userid=' + user,
dataType: 'json'
});
return def.promise().done(
function() {
console.log('done');
}).fail(
function() {
console.log('fail');
});;
}
var consoleProcessor = function (data) {
console.log(data.cohort);
}
var screenProcessor = function (data) {
$('#yourCohort').html('Your cohort is "' + data.cohort + '"');
}
$('#getCohortButton').on('click', function () {
var name = $('#username').val();
if (name) {
getCohort(name).done(screenProcessor);
$('#postPara').html(experimentSummary);
}
})