Link to home
Start Free TrialLog in
Avatar of prashanth ag
prashanth ag

asked on

How Close unsubmited attempts

1. I need alert pop message before 5 seconds.
0                          0                   5
Hours           Minutes     Seconds

2.submission of quiz attempts that students have forgotten to submit ,I needed to close some quiz attempts automatically
my case call  function submit_quiz()     if timer shows 00:00:00 & redirect to  "Home.php";

<script type="text/javascript">
    
    $(document).ready(function() {
	QS = 0;
	current = 0;
	
	startDate = "<?php echo $end_date ?>";
	startDate = startDate.replace(/:| /g,"-");
	var YMDhms = startDate.split("-");
        var sqlDate = new Date();
        //alert(sqlDate);
        duration = "<?php echo $EXAMS['duration']?>";

		//alert(duration);
        //dus = duration.split(':');
        dus = [0,0,0,0];
        sqlDate.setFullYear(parseInt(YMDhms[0]), parseInt(YMDhms[1])-1,
                                                 parseInt(YMDhms[2]));
        sqlDate.setHours(parseInt(YMDhms[3])+parseInt(dus[0]), parseInt(YMDhms[4])+parseInt(dus[1]), 
                                              parseInt(YMDhms[5])+parseInt(dus[2]), 0/*msValue*/);
     //  alert(sqlDate);
     

     $('#defaultCountdown').countdown({until: sqlDate, format: 'dHMS', timezone: +330});

  });
   </script>
window.submit_quiz = function submit_Quiz(){
            sid = <?php echo $sid;?>;
            att_id=<?php echo $att_id; ?>;
            $.post('cores/quiz.submit.php', {sid: sid, att_id: att_id}, function(data){
                if(data.indexOf('OK')!=-1){
                    alert("Quiz submitted successfully!");
                    window.location = "Home.php";
	            return;
	        }
	        alert(data);
            });
        }

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Here's an example of a JavaScript timer.  Five seconds may not be enough time for a (human) client to react.  Just a thought.
<?php // demo/countdown_timer.php
/**
 * Create a JavaScript countdown timer based on a PHP variable
 */
error_reporting(E_ALL);

// SET THE TIMER VALUE IN SECONDS THE PHP SCRIPT
$timer = 5;
?>

<script>
var interval = "";
var sekonds  = <?php echo $timer; ?>;

function startInterval(){
    interval = window.setInterval("tTimer()",1000);
}

function stopInterval(){
    window.clearInterval(interval);
    interval="";
}

function tTimer(){
    document.thinkTime.tDisplay.value = sekonds--;
    if (sekonds == -1)    {
        stopInterval();
    }
}
</script>

<script>
document.write('<meta http-equiv=\"Refresh\" content=\"<?php echo $timer; ?>;url=http://lmgtfy.com?q=JavaScript+Timer\">');
</script>

<script>startInterval();</script>

<form name="thinkTime">
This page will automatically redirect and search for a JavaScript Timer in

<input readonly class="timerBox" size="1" type="text" name="tDisplay" value="">

seconds.
</form>
<a href="http://lmgtfy.com?q=JavaScript+Timer">Click here if you don't want to wait.</a>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Julian Hansen
Here is some sample code
<script>
$.countDownTimer = function(options) {
  var then = (new Date()).getTime();
  var prompt = false;
  var intoptions = {
    timeout: 10000,
    prompt: 5000,
    url: ''
  }
  var opts = $.extend({}, intoptions, options);
  
  var timer = setInterval(intFn, 1000);
  
  function intFn() {
    var elapsed = (new Date()).getTime() - then + 1000;

    if (elapsed >= (opts.timeout - opts.prompt)) {
      if (elapsed >= opts.timeout) {
	    clearInterval(timer);
        window.location = opts.url;
      }
      else {
        if (prompt) {
          updateTimer(Math.ceil((opts.timeout - elapsed - 50) / 1000));
        }
        else {
          showPrompt(Math.ceil((opts.timeout - elapsed) / 1000));
        }
      }
    }
  }
  
  function updateTimer(val)
  {
    $('.countdown', prompt).html(val);
  }
  
  function hidePrompt()
  {
    $('.countdownPrompt').remove();
    prompt = false;
  }
  
  function showPrompt(init)
  {
    var p = '<div class="countdownPrompt">' + 
      '<p>Page will expire in <span class="countdown">' + init + '</span> seconds</p>' + 
      '<div class="text-center"><button class="btn btn-warning" id="cancel">Cancel</button></div>' +
    '</div>';
    
    prompt = $('body').append($(p));
    
  }

  $(document).on('click keypress mousemove', function() {
    then = (new Date()).getTime();
    hidePrompt();
  });
}

$.countDownTimer({
  url: 'http://www.experts-exchange.com'
});
</script>

Open in new window

Working sample here
Avatar of prashanth ag
prashanth ag

ASKER

I used the mentioned code of both its not working in my pc & What are the other things needed to take care achieve the same.
 i post my code, please rectify the error
<?php
date_default_timezone_get();
if(count($res)==0 && $re > 0){
$duration=explode(":",$quiz[0]["duration"]);
$secs = $duration[0]*3600+$duration[1]*60;
$date = date("Y-m-d H:i:s", time() + $secs);
?>

Open in new window

ts not working in my pc
Not helpful - how is it not working - errors? Do you have a link where you put the implementation of the code so we can see how you have done it.

How does your last post relate to the question?.  That is server side PHP code - the question is about a timer on the page - trying to see how you are relating the two?
Both of the code works fine, but when I apply your code in my application, it not meet as per my requirement
it not meet as per my requirement
@prashanth ag, to get the best out of EE you need to provide us with as much information as you can. Comments like the above just waste time - they don't give us any further information about your problem and give us nowhere to go.

If a solution presented does not work or does not meet expectations you need to tell us how and why.

In this case - tell us how the code does not meet your requirements.
How & Why
1.$secs = $duration[0]*3600+$duration[1]*60;
       both variables    $secs   & $timer in seconds are mismatching.
2.convert exam duration in seconds , then pass seconds only if it reaches 5 Sec to variable $timer = 5;
That addresses only the PHP solution - the second solution was JavaScript based.
<?php 
$quiztime = 10;
?>
<script type="text/javascript">
var interval = "";
var sekonds  = <?php echo $quiztime; ?>;
startInterval();
function startInterval()
{
    interval = window.setInterval("tTimer()",1000);
}
function tTimer()
{
    document.quiztime.tDisplay.value = sekonds--;
  //if (sekonds == 5)
		if (sekonds == -1)
    {
        window.clearInterval(interval);
		interval = 0;
		alert('Call submit_quiz() here');
	    window.location = "http://localhost/timer/home.php";
	}
	}
</script>
<form name="quiztime">
This page will search for a JavaScript Timer in
<input readonly class="timerBox" size="1" type="text" name="tDisplay" value="">
seconds</form>

Open in new window