I want to add the timer which when gets input of ‘0’ from form field named “Duration” will set to count up like from i.e it will start from 0 sec and which has no limit untill user close the window of output
//adding count up timer after user select 0 as input value to duration field
var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
setInterval(setTime, 1000);
function setTime() {
++totalSeconds;
secondsLabel.innerHTML = pad(totalSeconds % 60);
minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));
}
function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}
The else part above is working perfectly fine as a COUNTDOWN timer if input to the field is greater than 0
In addition to this , for my purpose if the input value set to 0 then it should set COUNTUP timer
but this part is not working .
Where I am going wrong ?
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 ?
hey @Mittineague thanks for the suggestions , I think I got the way to fix , this issue.
the issue of countup timer is solved now and working perfectly fine.
BUT there is anothe small issue which is merged here in above comment about the time settting I am bit confused to adjust the time calculations , if possible , can you check that one ?
hey I have updated the code which is setting the time to each question by calculating with he help of input fields I have posted above cooment 6
updated script
//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, $duration * 60000 / $num_questions);
}
//showing next item afer calculated seconds from below
function showNextItem()
{
$nextItem && clearTimeout($nextItem);
$nextItem = setTimeout(showNextItem, timer / $num_operands);
var $nextItem = getCurrentSlide().find('.slide-item:not(.visible)').eq(0);
if ($nextItem.length)
{
$nextItem.addClass('visible');
return;
}
}
function init()
{
//timer for next element
$nextItem = setTimeout(showNextItem, timer / $num_operands);
$(".goto-next-slide").click(gotoNextSlide);
//timer for next question slide
timer = setTimeout(gotoNextSlide, $duration * 60000 / $num_questions);
$(".goto-prev-slide").click(gotoPrevSlide);
goToSlide(0);
}
$(document).ready(init);
I have created a formulae to calculate the time but its not working , where I am going wrong ?
I’m familiar with using && to represent AND for comparing conditions, but not on its own like this.
I am not that familiar with JS and mainly come in here to learn - I hadn’t realised that variable names starting with a $ were done in JS as well as PHP. It seems confusing to me, given that there is also some PHP code floating around.
I can’t see where your $duration and $num_questions JS variables are coming from, don’t you need to grab the values from the form somewhere?
my script.js file is included in a my home.php tag and in the tag of this home.php my form is exists.
my rest of the script is working fine but not the script for timer I have posted above
thats the main issue I am having. The time is not calculating by taking input value from form fields in a proper way it only setting timer for the time I specified there .
Where is the code for getting the input value from the form fields? I see you’ve got a formula using $duration and $num_questions, but I don’t see where you assign values to those variables.