phptimerusleep

Implementing Timer in PHP and Sleep function


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.


Solution

  • 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