Hi,

Everything is working fine except the start time. Once I click start, i want to see start time is ticking but start time does not change. I want future time static and start time dynamic.

Please advise.

<html>

<?PHP
$From = date("H:i:s");
echo "From  $From ";
$Minutes = 1;
echo "<br />";
$To = date("H:i:s", strtotime($From)+($Minutes*60));
?>

<SCRIPT language="JavaScript">

function newtext()
{
document.getElementById("stBtn").disabled = true;
starttime = '<?php echo $From ;?>';
futuretime = '<?php echo $To ;?>';

document.getElementById("start").innerHTML = starttime;
document.getElementById("future").innerHTML = futuretime;
setTimeout("moretext()",1 * 60000);
 }

function moretext()
{

document.getElementById("subBtn").disabled = true;
}

</SCRIPT>

</HEAD>

<BODY>

<FORM name="myform" method="post" action="sessionS1.php">

<table width='600' cellpadding='5' cellspacing='5' border='1'> 
<tr> <td> <p id="start"></p> </td> <td> <p id="future"></p>  </td> <td> </td> </tr>

<tr><td colspan=6 align='right'><INPUT class = "button" id="stBtn" TYPE="button" name= mytext value="StartExam" onClick="newtext

()"></td></tr> 

<tr><td colspan=6 align='right'><input type='submit' id="subBtn"value='Score' name='btn_score'></td></tr> 

</table> 

</FORM>

</body>
</html>
Member Avatar for diafol

From what I can see, moretext() isn't doing anything. You call it, but it just ensures that the button is still disabled. Move your scripts to the end of the page (just above </body>).

If you place document.write('hi'); or similar to the moretext() function, you'll see if it's actually being called. Alternatively, use alert() or console.log

Can you use setInterval / clearInterval for this?

Also the start time isn't strictly true as it depends on the button click, which could be a while after the time displayed. Why not just have a countup or countdown in seconds?

Just a question - will refreshing the page give the applicant another minute?

refresh gives me current time every 2 seconds but it refreshes the entire page. then i have to click start again.

moretext() is disabling score button after 1 minute. it is working fine. newtext() calls moretext().

I am not sure what you are trying to say. All I want the starttime to tick without refreshing the entire page.

Please help.

Member Avatar for diafol

I was just saying that you haven't got any routine for changing time. Here's some stuff wwhich may help. I'm no js guru, but here's an attempt at a count-up. You can get much better examples from searching Google.

<html>
<head>
</head>
<BODY>
<FORM name="myform" method="post" action="sessionS1.php">
<table width='600' cellpadding='5' cellspacing='5' border='1'> 
<tr> <td> <p id="start">0</p> </td> <td> <p id="future"></p>  </td> <td> </td> </tr>
<tr><td colspan=6 align='right'><INPUT class = "button" id="stBtn" TYPE="button" name= mytext value="StartExam" onClick="newtext
()"></td></tr> 
<tr><td colspan=6 align='right'><input type='submit' id="subBtn"value='Score' name='btn_score'></td></tr> 
</table> 
</FORM>
<SCRIPT language="JavaScript">

var starttime = 0;
var futuretime = 20;

document.getElementById("future").innerHTML = futuretime;

function newtext()
{
    document.getElementById("stBtn").disabled = true;

    var goClock=self.setInterval(function(){
        if(starttime >= futuretime){
            self.clearInterval;
            document.getElementById("stBtn").disabled = false;
        }else{
            changeClock();
        }
    },1000);
}
function changeClock()
{
    starttime++;
    document.getElementById("start").innerHTML = starttime;
}
</SCRIPT>

Thanks much. Thats a lot better.

But I would be pleased if you could help me for the below:

  1. How can i change 0 to currenttime
  2. Then add 5 minutes to currenttime = futuretime
  3. then it will run from current time till 5 minutes after.
  4. it displays current time and futuretime

    <SCRIPT language="JavaScript">
    
    var thetime=new Date();
    var nhours=thetime.getHours();
    var nmins=thetime.getMinutes();
    var nsecn=thetime.getSeconds();
    
    
    var starttime = nmins;
    var maxm = nmins + 5*60 ;
    var futuretime = maxm;
    document.getElementById("future").innerHTML = futuretime;
    function newtext()
    {
        document.getElementById("stBtn").disabled = true;
        var goClock=self.setInterval(function(){
            if(starttime >= futuretime){
                self.clearInterval;
                document.getElementById("stBtn").disabled = false;
            }else{
                changeClock();
            }
        },1000);
    }
    function changeClock()
    {
        starttime++;
        document.getElementById("start").innerHTML = starttime;
    }
    </SCRIPT>
    
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.