Timer
-
Hi
I see there is a timer for the quiz as a whole, is there any way it can be done per question? Like 10/15 seconds per question?
Just thinking how to prevent cheating.
Thanks
-
Hi again jarman22,
this is not something built into HD Quiz, but this should be fairly simple to add in. When I have some spare time over the weekend I’ll see about writing a simple function that you can add to your theme’sfunctions.phpfile that will extend HD Quiz do this.I very much appreciate it, obviously whenever you do have some spare time, please don’t go out if your way if there are more important things to be doing.
Have a great weekend.
Check this out!
In the below script, you will see a line
const hdq_time_per_question = 25;
Change the25to however many seconds you want to give a user to select a question. If they do not answer within this time frame, then HD Quiz will auto go to the next question.NOTE: This will ONLY work if you are using the primary pagination method built into every question – like most things, this is NOT compatible with WP Pagination. So if you are using WP Pagination, please remove it and just use teh pagination option built into each question.
function hdq_per_question_timer() { ?> <div id = "hdq_per_question_timer" style = "position: fixed; bottom: 1rem; right: 1rem; padding: 1rem; background:#eee;"></div> <script> const hdq_timer_el = document.getElementById("hdq_per_question_timer"); const hdq_time_per_question = 25; function hdq_per_question_timer(){ let time_left = hdq_time_per_question; let timems = hdq_time_per_question * 1000; // convert seconds to ms let timer = setInterval(function(){ if(time_left < 0){ time_out_next_question(); } else { hdq_timer_el.innerHTML = time_left; } time_left = time_left - 1; }, 1000); function time_out_next_question(){ if(jQuery(".hdq_next_button:visible")[0]){ jQuery(".hdq_next_button:visible").click(); } else { jQuery(".hdq_finsh_button:visible").click(); clearInterval(timer); hdq_timer_el.remove(); } time_left = hdq_time_per_question; } // reset timer if an answer has been selected jQuery(".hdq_label_answer").on("click", function(){ time_out_next_question(); // we want to auto goto the next question }) } hdq_per_question_timer(); </script> <?php } add_action("hdq_after", "hdq_per_question_timer");Thank you, I will give this a go! Appreciate your quick response and doing this over the weekend!
Hi
Sorry to bother you, the timer works brilliantly but I noticed that should the “Next” button is clicked manually, the timer is still counting down during the next question.
I have disable the WP Pagination and have Paginated per question as you mentioned, works a treat but not sure why the timer is still counting down when you press “Next”.
Confused!
Ah good catch! I overlooked that. I’ll have to edit the code and resend.
I’ll make it so that if a user selects next without actually answering anything, the timer will restart for the new question as well.
Here is teh updated code that looks for if the user selects the next or finish button as well!
function hdq_per_question_timer() { ?> <div id = "hdq_per_question_timer" style = "position: fixed; bottom: 1rem; right: 1rem; padding: 1rem; background:#eee;"></div> <script> const hdq_timer_el = document.getElementById("hdq_per_question_timer"); const hdq_time_per_question = 25; function hdq_per_question_timer(){ let time_left = hdq_time_per_question; let timems = hdq_time_per_question * 1000; // convert seconds to ms let timer = setInterval(function(){ if(time_left < 0){ time_out_next_question(); } else { hdq_timer_el.innerHTML = time_left; } time_left = time_left - 1; }, 1000); function time_out_next_question(){ if(jQuery(".hdq_next_button:visible")[0]){ jQuery(".hdq_next_button:visible").click(); } else { jQuery(".hdq_finsh_button:visible").click(); clearInterval(timer); hdq_timer_el.remove(); } time_left = hdq_time_per_question; } // reset timer if an answer has been selected jQuery(".hdq_label_answer").on("click", function(){ time_out_next_question(); // we want to auto goto the next question }); jQuery(".hdq_next_button").on("click", function(){ time_left = hdq_time_per_question; }); jQuery(".hdq_finsh_button").on("click", function(){ clearInterval(timer); hdq_timer_el.remove(); }); } hdq_per_question_timer(); </script> <?php } add_action("hdq_after", "hdq_per_question_timer");Hi
Thanks for doing this late last night!
It seems now that each time I click on “Next”, it skips the next question and go straight to the following one.
So at Question 1. I’ll tick the answer I think is right, click on “Next”, it skips Question 2. straight to Question 3. Again, I’ll answer this one click “Next” it goes to Question 5. skipping the 4th.
Any idea on what I could be doing wrong?
Thanks
Can you please send me a link to a quiz? I’m not able to replicate this issue on my side.
For me, if I am on Question #1 and either select and answer or select the next button, we automatically go to Question #2
Sure
Here’s the link:- https://jarbinator.com/quiz/music/
I think the timer is working now, not sure what happened this morning. I’ve noticed that the timer starts even before question 1 comes up. Also I noticed that when the quiz is finished, it shows the correct and wrong answers apart from the last Question where it doesn’t show either.
Do not enable pagination on the first question.
Pagination basically means “start a new page with this question”. This is why you have to press “next” to see the first question.
ok, I’ve removed pagination for the 1st Question. I’m still not getting the last question to work, so if I select the right answers for 15 Questions, it shows 14/15 correct. Any ideas? Sorry π
I’m pretty sure that it’s just a bug with the question timer. I’ll look into it and let you know asap π
Heeeeeerrrreeee we go. Think I got it working again now.
function hdq_per_question_timer() { ?> <div id = "hdq_per_question_timer" style = "position: fixed; bottom: 1rem; right: 1rem; padding: 1rem; background:#eee;"></div> <script> const hdq_timer_el = document.getElementById("hdq_per_question_timer"); const hdq_time_per_question = 25; function hdq_per_question_timer() { let time_left = hdq_time_per_question; let timems = hdq_time_per_question * 1000; // convert seconds to ms let timer = setInterval(function() { if (time_left < 0) { time_out_next_question(); } else { hdq_timer_el.innerHTML = time_left; } time_left = time_left - 1; }, 1000); function time_out_next_question() { if (jQuery(".hdq_next_button:visible")[0]) { jQuery(".hdq_next_button:visible").click(); } else { clearInterval(timer); hdq_timer_el.remove(); setTimeout(function() { jQuery(".hdq_finsh_button").click(); }, 500); } time_left = hdq_time_per_question; } // reset timer if an answer has been selected jQuery(".hdq_label_answer").on("click", function() { time_out_next_question(); // we want to auto goto the next question }); jQuery(".hdq_next_button").on("click", function() { time_left = hdq_time_per_question; }); jQuery(".hdq_finsh_button").on("click", function() { clearInterval(timer); hdq_timer_el.remove(); }); } hdq_per_question_timer(); </script> <?php } add_action("hdq_after", "hdq_per_question_timer");Yes! What a great job! I can’t thank you enough and really appreciate your help over the last few days it hadn’t gone unnoticed.
I certainly would recommend your work highly.
I’ll look forward to seeing some more of your new features in future!
Would be good (if you haven’t already) have like a update post or something to inform us of the possible new plugin etc.
Thanks again!
The topic ‘Timer’ is closed to new replies.