I am building a web page for a website that will be used as psychological experiment (for gathering data from users).
The page should act in this order:
1. Show plus image for 250 ms.
2. Show am image from an array of images for 750 ms.
3. Show a form containing a question on which the user is suppose to answer, and so on.
I need to iterate those steps while i haven't finished to show all the images at my array.
HTML code i have:
<img src="http://brain.netii.net/images/Blank.png" id="pictures" name="pictures"/>
<form id="inputs" action="#" class="validate" method="post">
<label class="quesLabel" for="assoInput1">First association:</label>
<div>
<input required pattern="([a-z]*|[A-Z]*|[ \t]*)*" type="text" id="assoInput1" name="asso1" title="please enter first association" autocomplete="off" value="">
</div>
<br>
<input type="submit" value="Submit">
</form>
CSS code i have:
#container
{
width: 960px;
padding: 0;
margin: 0 auto;
}
#pictures
{
width: 960px;
height: 720px;
padding: 0;
}
#inputs
{
width: 960px;
height: 720px;
margin: 100px 50px;
display: none;
}
JS code i have:
window.onload = function()
{
preloader();
pictureImg = document.getElementById("pictures");
inputForm = document.getElementById("inputs");
inputForm.onsubmit = function()
{
//alert('Form: onsubmit func');
LoadPlus();
};
LoadPlus();
//LoadImage();
};
function LoadPlus()
{
inputForm.style.display="none";
//stop condition:
if (imagesArr.length <= 0) //hope that here is no error here
{
//alert("All images loaded");
window.location.href = "start_experiment.html";
}
var url = plus;
var img = new Image();
img.src = url;
img.onload = function()
{
//alert("Plus loaded.");
setTimeout(LoadImage, 250);
};
pictureImg.src = img.src;
pictureImg.style.display="block";
}
function LoadImage()
{
var randomNum;
if (imagesArr.length > 0)
{
randomNum = Math.floor(Math.random() * imagesArr.length);
console.log(imagesArr[randomNum]);
}
else
{
alert ("ERROR: out of array boundaries!");
}
var url = "http://brain.netii.net/images/Practice" + imagesArr[randomNum] +".png";
imagesArr.splice(randomNum, 1);
var img = new Image();
img.src = url;
img.onload = function()
{
//alert("image #" + randomNum + " loaded, loading next..");
setTimeout(LoadForm, 750);
};
pictureImg.src = img.src;
}
function LoadForm()
{
//blank only pictures div
inputForm.reset();
inputForm.style.display="block";
pictureImg.style.display="none";
//alert('Form loaded');
}
Here you can see the page.
My problem starts on submitting the form, the onload trigger loads the plus image twice instead of once (sometimes the first plus shown for a short amountt of time and sometimes it even shows the picture that shuld be seen after the plus).
Is there another way to implement this? Could anyone please assist me?
Thank you, Max.