1

I am creating an PHP application in which i want to set a timer of 15 minutes for a particular section of test. If the student doesn't complete the section in 15 minutes the scores are saved and the student is navigated to the next section.

So i want to create a timer and want to display the current time left. But i don't know how to implement it and also if we use sleep() function that will it interrupt the working of other functions as well.

3
  • 1
    this can be done via JS Commented Mar 11, 2017 at 6:21
  • But i cant save the scores of students in Db via JS Commented Mar 11, 2017 at 6:25
  • using ajax you can save students scores in database. Commented Mar 11, 2017 at 6:26

2 Answers 2

2

You can't use PHP for this. If you do, then it will simply freeze the page on page load for 15 seconds and then tell the student time has expired.

So, you should write it in JavaScipt like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
currentMinute = 0;
myTimer = setInterval(function () {
    if (currentMinute > 15) {
        clearInterval(myTimer);
        //send post data that the user's time is up
        return;
    }
    $.post("sendData.php", {
        "timeIsUp" : true
    }, function (data) {
        console.log("done sending time has expired to server");
    });
    currentMinute++;
}, 60 * 1000);
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

But i cant save the scores of students in Db via JS
I added some code that will send data to a file called sendData.php like a form would in html when the time has expired.
You would insert your code to add it to the database inside sendData.php
Can you please explain the working of this function?
Sure, okay, so in 15,000 milliseconds (or seconds, or 15 * 1000 milliseconds), we access sendData.php with the $_POST['timeIsUp'] equal to true. Then, when we are done sending that information, we print "done sending..." to console.
|
0

jQuery will help you on this...

Use below function for timer

var hours1=0, minutes1=0, seconds1=0;

function calculate() {

setTimeout(calculate, 1000);
if((hours==0)&&(minutes==0)&&(seconds==0)){$('#submit-btn').trigger('click');} // HERE YOU CAN SWITCH TO NEXT QUESTION
h1 = makeTwoDigit(hours);   m1 = makeTwoDigit(minutes); s1 = makeTwoDigit(seconds);
h2 = makeTwoDigit(hours1);  m2 = makeTwoDigit(minutes1);    s2 = makeTwoDigit(seconds1);
$('title').html(h1+':'+m1+':'+s1);
$('#time-taken').val(h2+':'+m2+':'+s2);
$('#minutes').html(m1);
$('#seconds').html(s1);

seconds--; 
if (seconds < 0) { 
    seconds = 59; minutes--;

    if (minutes < 0) {
        hours--;
        minutes = 59;                        
    }
}
seconds1++; 
if (seconds1 > 59) { 
    seconds1 = 00; minutes1++;      
    if (minutes1 >59) {
        hours1++;
        minutes1 = 00;                        
    }
}

}

function makeTwoDigit(num) {
    if (num < 10) { return "0" + num;} return num;
}

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.