Please help me I am stuck on .How to set javascript timer with proper calculated time bsed on form inputs?

  • These are my form fields using input given to these fields I want to set the timer

        <div class="row">
              <div class="col-25">
                  <label>Duration:</label>
              </div>
              <div class="col-75">
                  <input type="number" id="duration" name="duration" value="0" min="0">
              </div>
       </div>
       <div class="row">
              <div class="col-25">
                  <label for="num_questions">Select No. of questions:</label>
              </div>
              <div class="col-75">
                  <input type="number" id="num_questions" name="num_questions" value="1" min="1" max="100">
              </div>
          </div>
      <div class="row">
              <div class="col-25">
                  <label for="int">How many numbers you want in a sequence? :</label>
              </div>
              <div class="col-75">
                  <select name="num_operands">
                      <option value="2"> 2 </option>
                      <option value="3"> 3 </option>
                      <option value="4"> 4 </option>
                      <option value="5"> 5 </option>
                      <option value="6"> 6 </option>
                      <option value="7"> 7 </option>
                      <option value="8"> 8 </option>
                      <option value="9"> 9 </option>
                      <option value="10"> 10 </option>
                  </select>
              </div>
          </div>
    

    My javascript code for timer

         //script for timer countdown
                 function secondPassed() 
                    {
                         var minutes = Math.round((seconds - 30)/60),
                          remainingSeconds = seconds % 60;
                           if (remainingSeconds < 10) 
                            {
                             remainingSeconds = "0" + remainingSeconds;
                            }
                        document.getElementById("countdown").innerHTML = minutes + ":" + remainingSeconds;
                           if (seconds == 0) 
                             {
                                clearInterval(countdownTimer); 
                                window.location.href = 'http://localhost/copied/final_generate/slideshow.php';
                             } 
                         else
                            {
                             seconds--;
                            }
                    }
                       var countdownTimer = setInterval('secondPassed()', 1000);
    

This iis where I am displaying it

           <div class="timer" style= 'font-size: 25px;color:  #ffe6e6;'>  
             <?php echo "Duration:" ?>
             <time id="countdown"><script> var seconds = <?php echo  $duration * 60; ?>;</script>  <?php echo $duration; ?>
             </time>
          </div>             

This is script I have created for switching no.of questions after 1 minute

          /script for generate one by one questions
         var timer;
        // go to next question
       function gotoNextSlide()
      { 
       timer && clearTimeout(timer);
       goToSlide(getCurrentSlide().index() + 1);
        timer = setTimeout(gotoNextSlide, 1000 * 60);
      }                 
       //show current question
     function getCurrentSlide() 
  {
      return $("#slides .slide:visible");
  }
     //showing next item or number in current question afer 3 second
    function showNextItem() 
   {
     $nextItem && clearTimeout($nextItem);
     $nextItem = setTimeout(showNextItem, 3000);
     var $nextItem = getCurrentSlide().find('.slide-item:not(.visible)').eq(0);  
      if ($nextItem.length) 
    {
      $nextItem.addClass('visible');
       return;
    }  
 } 
   
  function init() 
   {
    //timer for next item or number
    $nextItem = setTimeout(showNextItem,  3000);
    $(".goto-next-slide").click(gotoNextSlide);
   //timer for next question slide
   timer = setTimeout(gotoNextSlide, 1000 * 60);

goToSlide(0);
}

   $(document).ready(init);

My Aim -

  • the first field is of duration , user can give any input to this field for ex he gave input of 4 then 4 minute timer should set.
  • then the no.of questions field , suppose user gave input of 2 the 4 minute timer should set with equal distribution of time to both the questions i.e 120seconds to each question
  • if user gave input of 3 for how many numbers he want in a question then that previous fields 120 seconds should split to adjust these 3 numbers i.e 40 seconds wil be set for each number

NOTE - This is for basically a math quiz in which the questions will be automatically slides(there is a slideshow in the output I am displaying each item in a questions one by one ) using timer along with the numbers inside the questions based on input.

  • Everything is working here but only the timer settings are not working , when I gave input to duration field then the timer is set properly for that input but not for each question , its setting for whole set of questions
  • I have set those 3000(3seconds) just to show for example
    I tried some calculation but they are not helping , How can I aply the calculatins in my script ?

A post was merged into an existing topic: Problem with the count up timer in javascript