I have a few more questions. I am having trouble with buttons.
-I am trying to add a "Start" button before setInterval is activated. I basically don't want numbers to be shown before player clicks "Start" but it doesnt work.
- I also want to add a "Next" button which will appear after "Buy" is clicked and it will direct users to the Results page. (currently "Buy" button directs players to Results page).
I attached the JS, html and CSS codes below, I tried most of the suggestions on forums but couldnt make it work.
JS File ###################################################################################
var current_input = 1;
var value_array = ["$20","$21",'$11','$14','$43','$16','$49','$27','$40','$25','$45','$52','$29','$39','$42','$41','$66','$50','$75','$68'];
var value_array2 = [20,21,11,14,43,16,49,27,40,25,45,52,29,39,42,41,66,50,75,68];
var time_interval = 3000;
var time_value=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21];
var value_array_position = 0;
var selectionnot_made=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var selectionmade=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,];
var periodcounter = 0;
window.onload = function(){
time_period=0;
// Create desired number of inputs, with the desired offset (in pixels)
createInputs(20, 4);
//var startButton = document.getElementById("start");
// startButton.onclick = function() {
myInterval = setInterval(function(){
time2 = time_value[value_array_position];
document.getElementById("demo").innerHTML =time2;
document.getElementById("currentprice").innerHTML =value_array[value_array_position];;
// Disable (grey out) previous input
if(document.getElementById("input-range-"+(current_input-1)) != null) {
document.getElementById("input-box-"+(current_input-1)).disabled = true;
document.getElementById("input-range-"+(current_input-1)).disabled = true;
}
if(document.getElementById("input-range-"+(current_input)) != null){
// For random value
//new_value = Math.round(Math.random() * 50);
// For value from array
new_value = value_array2[value_array_position];
new_valuestr = value_array[value_array_position];
time = time_value[value_array_position];
value_array_position++;
// if(value_array_position >= value_array.length) {
// value_array_position = 0;
// }
// Set new values to elements
document.getElementById("input-range-"+current_input).value = new_value;
document.getElementById("input-box-"+current_input).value = new_valuestr;
// set the new price to the hidden element
document.getElementById("selected_price").value = new_value;
document.getElementById("time_period").value = time;
document.getElementById("selection_made").value = selectionmade[value_array_position];
current_input++;
}
else {
document.getElementById("selection_made").value = selectionnot_made[value_array_position];
document.getElementById("end-button").click();
clearInterval(myInterval);
}
}, time_interval);
};
function createInputs(numInputs, offsetPixels=4, middleSpacing=10){
container = document.getElementById("inputs-container");
verticalOffset = offsetPixels * (numInputs-1) + middleSpacing;
maxrange=40;
minrange=0;
for(i=1; i<=numInputs; i++){
container.innerHTML += '<div class="input-container"><i> t'+i+'</i> <input type="text" class="input-box" id="input-box-' + i + '" style="margin-bottom:' + verticalOffset + 'px;" readonly/> ' + maxrange + ' <br><input type="range" class="input-range" id="input-range-' + i + '" min='+minrange+' max= '+maxrange+' step="0" value=0 sorient="vertical" readonly/>'+minrange+'</div>';
verticalOffset -= offsetPixels;
maxrange=40+2*i;
minrange=2*i;
}
}
HTML File###################################################################################
{% extends "global/Base.html" %}
{% load staticfiles otree %}
{% load i18n %}
{# ****************************************************************************************************************** #}
{# *** STYLES *** #}
{# ****************************************************************************************************************** #}
{% block styles %}
<link href="{% static 'bret/css/bret.css' %}" rel="stylesheet"/>
<link href="{% static 'bret/css/custom.css' %}" rel="stylesheet"/>
<link href="{% static 'bret/css/fontawesome-all.css' %}" rel="stylesheet"/>
<style type="text/css">.ng-cloak{display:none !important;}</style>
<script src="{% static 'bret/js/angular.min.js' %}"></script>
<script src="{% static 'bret/js/bret.js' %}"></script>
{% endblock %}
{# ****************************************************************************************************************** #}
{# *** SCRIPTS *** #}
{# ****************************************************************************************************************** #}
{% block scripts %}
{% endblock %}
{# ****************************************************************************************************************** #}
{# *** TITLE *** #}
{# ****************************************************************************************************************** #}
{% block content %}
<h2>Time Period: <span id="demo", ></span> </h2>
<h2>Current Price: <span id="currentprice", ></span> </h2>
<div id="inputs-container"
> </div>
<input type="hidden" name="selected_price" id="selected_price" />
<input type="hidden" name="time_period" id="time_period" />
<input type="hidden" name="selection_made" id="selection_made" />
<button id="start" onclick="" >start</button>
<button id="next-button" onclick="">Buy</button>
<button id="end-button" onclick="", style="display:none" >Stop</button>
<button id="resutpage-button" onclick="", style="display:none" >Next</button>
<input type='hidden', class="counter" name="time_of_decision" id="p.time_of_decision()" />
{% endblock %}
CSS File ###################################################################################
.input-range {
-webkit-appearance: slider-vertical;
width: 20px;
height: 150px;
border: 0.2px solid #ab2c6c;
border-radius: 1px;
cursor:pointer;
background: transparent;
border-color: transparent;
color: transparent;
}
.input-box {
width: 32px;
margin-bottom: 10px;
text-align: center;
background: #d4e2fc;
opacity=0.2;
}
.input-container {
margin: 10px;
text-align: center;
font-size: 12px;
width: 27px;
pointer-events: none;
display: inline-block;
vertical-align: top;
}