Search code examples
javascriptzend-frameworkzend-formcountdown

Zend Countdown Timer


I'm working on a simple game which consists of consecutive questions and at the end of 30 questions the page redirects to another page (from play action to finish action in question controller). I am having difficulty in creating a countdown timer in javascript (that will limit the total gameplay time to 240 seconds and show that countdown to the player) which can keep the counter value of the countdown between successive answer Posts to the questions. (i.e. when the game starts it begins counting back from 240 and when the first question is answered and posted the second question will be presented and the countdown timer will continue from 240 - (the time spent on 1st question) and so on.)

    public function playAction() {
    $sess = new Zend_Session_Namespace("mysession");

    $qlist = $sess->qlist;
    $qindex = $sess->currentQuestion;
    $form = new Application_Form_QuestionForm();

    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
            $ans = $form->getValue('Answer');
            $uid = Zend_Auth::getInstance()->getIdentity()->id;
            $qid = $qlist[$qindex]['id'];
            $qtype = $qlist[$qindex]['type'];
            $model = $this->_getUserQuestionModel();

            $model->addMe($uid, $qid, $qtype, $ans);
            $qindex++;
            $sess->currentQuestion = $qindex;
        } else {
            $form->populate($formData);
        }
    }
    if ($qindex > 29) { // all questions were asked
        $this->_helper->redirector('finish');
    }
    $this->view->form = $form;
    $this->view->qname = "Question " . ($qindex + 1) . ": " . $qlist[$qindex]['verb'] . "  --->  " . $qlist[$qindex]['passive'];
}

Thanks,


Solution

  • Have you tried submitting the timer value(seconds passed) when you submit the answer and then when generating the page with the question instead of hard coded 240 echo the value with php ?

    using jquery javascript library

    $('#myform').bind('submit', function()
    {
    
    var timer = $('#timer').val()
    
    $('#myform').append('<input type="hidden" value="'+ timer +'" name="timer" />');
    
    });