PDA

View Full Version : [beginner] Looking for help



Mana
05-01-2014, 05:54 AM
I am a beginner learning Javascript. I try to use alert boxes to alert the users, also, i try to set a hidden timer in my page. But all the codes are not working in my page especially the timer.
Please help me to solve this.:(



<script type="text/javascript">

var d=new Date();
var time=d.getHours();

if (time <=12){
alert("Good Morning! Please enter your name and fill in all the questions.");
}

else if(time >12 && time <18){
alert("Good Afternoon! Please enter your name and fill in all the questions.");
}

else{
alert("Good Evening! Please enter your name and fill in all the questions.");
}

var start_time, end_time, time_spent, k;


function startTimer(){
k = new Date();
start_time = k.getTime();
alert("The timer is started! Please answer the questions ASAP!!");
}


function stopTimer(){
k = new Date();
end_time = k.getTime();
time_spent = end_time - start_time;
alert("you had used " .time_spent. " to finish this form.");

document.getElementById("hidden_timer").value = time_spent;
}

function validateForm() {
var x=document.forms["quizform"]["username"].value;

if (x =="" || x ==null){
alert("Please enter your name.");
return false;
}
else{
return confirm("Are you sure to send the form?");
}
}
</script>

vwphillips
05-01-2014, 10:48 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<form name="quizform" >
<input name="username" />
<input id="hidden_timer" />
<input type="button" name="" value="validateForm()" onclick="validateForm();"/>
</form>
<script type="text/javascript">

var d=new Date();
var time=d.getHours();

if (time <=12){
alert("Good Morning! Please enter your name and fill in all the questions.");
}

else if(time >12 && time <18){
alert("Good Afternoon! Please enter your name and fill in all the questions.");
}

else{
alert("Good Evening! Please enter your name and fill in all the questions.");
}

var start_time;


function startTimer(){
start_time = new Date().getTime();
alert("The timer is started! Please answer the questions ASAP!!");
}


function stopTimer(){
var time_spent = new Date().getTime() - start_time;
alert("you had used "+time_spent+" to finish this form.");

document.getElementById("hidden_timer").value = time_spent;
}

function validateForm() {
var x=document.forms["quizform"]["username"].value;

if (x ==""){
alert("Please enter your name.");
return false;
}
else{
stopTimer();
return confirm("Are you sure to send the form?");
}
}

startTimer()
</script>
</body>

</html>