Countdown Javascript isn't working on Chrome Extension - javascript

I'm new to making extensions and this is driving me crazy. My countdown works when I load the page locally in the browser, but when I try to make it an extension, only the html shows, and the javascript doesn't work (ex. When I click on the start button when launching the extension, nothing changes and the countdown never starts.) I put my code below, dows it have somethning to do with me not putting the javascript in the "background"? I didnt understand that part of the Chrome Docs
<html>
<head>
</head>
<body>
<script>var countdown;
var countdown_number=10000*3600
var days;
var hours;
var minutes;
var seconds;
function countdown_init() {
//countdown_number = 11;
countdown_trigger();
}
function countdown_trigger() {
if(countdown_number > 0) {
countdown_number--;
//store()
days = Math.floor(countdown_number/(3600*24))
hours = (Math.floor(countdown_number/(3600))-days*24) % 24;
minutes = (Math.floor(countdown_number/(60))-hours*60) % 60;
seconds = (Math.floor(countdown_number)-minutes*60) % 60;
update_counter();
if(countdown_number > 0) {
countdown = setTimeout('countdown_trigger()', 1000);
}
}
}
function update_counter(){
document.getElementById('timer_text').innerHTML = "Days: "+days+"<br>"+
" Hours: " + hours +"<br>"+" Minutes: " + minutes +"<br>"+" Seconds: " + seconds;
}
function countdown_clear() {
clearTimeout(countdown);
}
function countdown_reset(){
countdown_number=10000*3600;
update_counter();
clearTimeout(countdown);
}
function writeItem(){
localStorage[1] = countdown_number;
}
function returnItem() {
var stored = localStorage[1];
document.getElementById('item').innerHTML=countdown_number;
}
function store(){
writeItem();
}
</script>
<div>
<h1> 10,000 Hours Timer </h1>
<input type="button" value="start countdown" onclick="countdown_init()" />
<input type="button" value="stop countdown" onclick="countdown_clear()" />
<input type="button" value="reset" onclick="countdown_reset()"/>
<input type="button" value="store" onclick="store()"/>
<p id="item">Hi</p>
</div>
<div id="timer_text">Ready To Start?</div>
</body>
</html>

Chrome extension doesn't support inline JavaScript.
http://developer.chrome.com/extensions/contentSecurityPolicy.html
Try this JS:-
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('#start').addEventListener('click', countdown_init);
document.querySelector('#stop').addEventListener('click', countdown_clear);
document.querySelector('#reset').addEventListener('click', countdown_reset);
document.querySelector('#store').addEventListener('click', store);
});
var countdown_number=10000*3600;
var days;
var hours;
var minutes;
var seconds;
var countdown;
function countdown_init(e) {
//countdown_number = 11;
countdown_trigger();
}
function countdown_trigger() {
if(countdown_number > 0) {
countdown_number--;
//store()
days = Math.floor(countdown_number/(3600*24));
hours = (Math.floor(countdown_number/(3600))-days*24) % 24;
minutes = (Math.floor(countdown_number/(60))-hours*60) % 60;
seconds = (Math.floor(countdown_number)-minutes*60) % 60;
update_counter();
if(countdown_number > 0) {
countdown = setTimeout(countdown_init, 1000);
}
}
}
function update_counter(){
document.getElementById('timer_text').innerHTML = "Days: "+days+"<br>"+
" Hours: " + hours +"<br>"+" Minutes: " + minutes +"<br>"+" Seconds: " + seconds;
}
function countdown_clear(e) {
clearTimeout(countdown);
}
function countdown_reset(e){
countdown_number=10000*3600;
update_counter();
clearTimeout(countdown);
}
function writeItem(){
localStorage[1] = countdown_number;
}
function returnItem() {
var stored = localStorage[1];
document.getElementById('item').innerHTML=countdown_number;
}
function store(e){
writeItem();
}
And HTML:-
<div>
<h1> 10,000 Hours Timer </h1>
<input type="button" id="start" value="start countdown" />
<input type="button" id="stop" value="stop countdown" />
<input type="button" id="reset" value="reset" />
<input type="button" id="store" value="store" />
<p id="item">Hi</p>
</div>
<div id="timer_text">Ready To Start?</div>

Related

My command prompt opens before the click on a button

I have 3 prompts, the first is to enter hours and the second minutes, when the user click on button:add n minutes, I would like to display a third prompt by proposing several minutes.
My problem is that my command prompt is executed before the clik on button.
My HTML
<body >
<h1>Exercise 8</h1>
<label>Hours : <input type="text" id='h' /></label>
<label>Minutes : <input type="text" id='m' /></label>
<button id="btn1">Pass 1 minute</button>
<button id="btn2">Add n minutes</button>
<script src="script.js"></script>
</body>
My JS
var hours = parseInt(prompt('Enter your hours please : '));
var minutes = parseInt(prompt('Enter your minutes please : '));
var btnPassOneMinute = document.getElementById('btn1');
var btnPass_N_Minute = document.getElementById('btn2');
/*if (hours != null) {
main();
}*/
btnPassOneMinute.addEventListener('click', addOneMinute);
btnPass_N_Minute.addEventListener('click', add_N_Minute);
main();
addOneMinute();
add_N_Minute();
function main(){
if(minutes > 59){
minutes = 0;
hours += 1;
}
if(hours > 23){
hours = 0;
}
document.getElementById('h').value = hours;
document.getElementById('m').value = minutes;
}
function addOneMinute(){
main();
minutes += 1;
}
function add_N_Minute(){
//main();
var addMinute = parseInt(prompt('Add your minutes : '));
minutes += addMinute;
if(addMinute > 59){
minutes = 0;
}
if(minutes > 59){
minutes = 0;
hours += 1;
}
if(hours > 23){
hours = 0;
}
document.getElementById('h').value = hours;
document.getElementById('m').value = minutes;
}
var hours = parseInt(prompt('Enter your hours please : '));
var minutes = parseInt(prompt('Enter your minutes please : '));
var btnPassOneMinute = document.getElementById('btn1');
var btnPass_N_Minute = document.getElementById('btn2');
/*if (hours != null) {
main();
}*/
btnPassOneMinute.addEventListener('click', addOneMinute);
btnPass_N_Minute.addEventListener('click', add_N_Minute);
main();
addOneMinute();
add_N_Minute();
function main(){
if(minutes > 59){
minutes = 0;
hours += 1;
}
if(hours > 23){
hours = 0;
}
document.getElementById('h').value = hours;
document.getElementById('m').value = minutes;
}
function addOneMinute(){
main();
minutes += 1;
}
function add_N_Minute(){
//main();
var addMinute = parseInt(prompt('Add your minutes : '));
minutes += addMinute;
if(addMinute > 59){
minutes = 0;
}
if(minutes > 59){
minutes = 0;
hours += 1;
}
if(hours > 23){
hours = 0;
}
document.getElementById('h').value = hours;
document.getElementById('m').value = minutes;
}
<!DOCTYPE html>
<html>
<head>
<title>Cours JavaScript</title>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, user-scalable=no">
<link rel="stylesheet" href="cours.css">
</head>
<body >
<h1>Exercise 8</h1>
<label>Hours : <input type="text" id='h' /></label>
<label>Minutes : <input type="text" id='m' /></label>
<button id="btn1">Pass 1 minute</button>
<button id="btn2">Add n minutes</button>
<script src="script.js"></script>
</body>
</html>
Thank you...

How to get child arrays in ajax html()

I have a form for my array of data will print to view with html() and this data each one has child array, I need to get data of those child arrays in my html() as well
Screenshots
my data
how it will look
# Code
HTML
<div class="answerPanel"></div>
<button id="clicks" class="btn btn-primary">Begin</button>
Script
var index = 0;
$("#clicks").click(function(){
// timer function
function timer(seconds, countdownTimer, callback) {
var days = Math.floor(seconds / 24 / 60 / 60);
var hoursLeft = Math.floor((seconds) - (days * 86400));
var hours = Math.floor(hoursLeft / 3600);
var minutesLeft = Math.floor((hoursLeft) - (hours * 3600));
var minutes = Math.floor(minutesLeft / 60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = hours + ":" + minutes + ":" + remainingSeconds;
if (seconds == 0) {
clearInterval(countdownTimer);
document.getElementById('countdown').innerHTML = "Times Up!";
$("#clicks").attr("disabled", true);
$('.answerPanel').html('<div class="text-center text-danger">OH NO! <br> Times Up!</div>');
} else {
seconds--;
}
//Pass seconds param back to the caller.
callback(seconds);
}
//We pass the countdownTimer param into the timer function as well.
var countdownTimer = null,
seconds = data.quizzes[index].quiz_time;
countdownTimer = setInterval(function() {
timer(seconds, countdownTimer, function(_seconds){
seconds = _seconds;
})
}, 1000);
// printing function
if(typeof data.quizzes[index] != 'undefined'){
var row = `<form>
<div class="row">
<div class="col-md-12">
<div class="pull-left questionTitle">
${data.quizzes[index].question}
</div>
<div class="pull-right" id="countdown"></div>
</div>
<div class="col-md-12">
Choice 1
</div>
<div class="col-md-12">
Choice 2
</div>
<div class="col-md-12">
Choice (etc.)
</div>
</div>
</form>`;
$('.answerPanel').html(row);
index++;
}
if(data.quizzes.length > index+1) {
$("#clicks").html("Next");
}
if(data.quizzes.length === index) {
$("#clicks").html("Finish");
}
//end of printing function
});
Any idea?
Please see my code below
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body id="banner">
<ul id="eventCal">
</ul>
<button id="clicks">Click</button>
<script type="text/javascript">
jQuery(document).ready(function($){
var quizzes = [{title:'title1',choices:[{choice:'choice1'},{choice:'choice2'}]},
{title:'title2',choices:[{choice:'new1'},{choice:'new2'}]},
{title:'title3',choices:[{choice:'demo1'},{choice:'demo2'}]}];
var index = 0;
//console.log(quizzes.length)
$("#clicks").click(function(){
if(typeof quizzes[index] != 'undefined'){
var html = '<li><span>'+quizzes[index].title+'</span></li>';
if(quizzes[index].choices.length > 0){
html+='<li class="choises">';
quizzes[index].choices.forEach((element, index, array) => {
//console.log(element.title);
html+='<ul>';
html+='<li><span>'+element.choice+'</span></li>';
html+='</ul>';
});
html+='</li>';
}
$("#eventCal").html(html);
index++;
}
if(quizzes.length === index)
$("#clicks").html("Finish");
})
});
</script>
</body>
Inside of row, you can get the choices by:
let choices = quizzes[index].choices

display and calculate score for javascript quiz

i'm a teacher and have just started learning to code for making online quizzes for my students. I'm still very new to programming like JavaScript and php, and I've tried to looked for sources online to help create my quizzes. I have 2 questions:
1). I've set a timer for the quiz but everytime when the time is up, it just keeps counting, what should I put in the
if (parseInt(min) == 0) {
clearTimeout(tim);
location.href = "";
section to redirect my student to the result page or other pages?
(2) My quizzes are mostly fill-in-the-blanks questions and I wonder how to store the point of each question and then show the total score to my students at the end of the quiz? Many thanks!.
Here's my code:
<html>
<head>
<script language ="javascript" >
var tim;
var min = 0;
var sec = 30;
var f = new Date();
function f1() {
f2();
document.getElementById("starttime").innerHTML = "Your started your quiz at " + f.getHours() + ":" + f.getMinutes();
}
function f2() {
if (parseInt(sec) > 0) {
sec = parseInt(sec) - 1;
document.getElementById("showtime").innerHTML = "Your Left Time is :"+min+" Minutes ," + sec+" Seconds";
tim = setTimeout("f2()", 1000);
}
else {
if (parseInt(sec) == 0) {
min = parseInt(min) - 1;
if (parseInt(min) == 0) {
clearTimeout(tim);
location.href = "www.rawlanguages.com";
}
else {
sec = 60;
document.getElementById("showtime").innerHTML = "Your Left Time is :" + min + " Minutes ," + sec + " Seconds";
tim = setTimeout("f2()", 1000);
}
}
}
}
</script>
<title>Quiz</title>
<h1>P.1 Grammar Quiz</h1>
<body>
<div id="ques0" class="ques">
<h2>Question</h2>
<p>She
<input type="text" name="answer0"/> a girl.</p>
</div>
<div id="ques1" class="ques">
<h2>Question</h2>
<p>"is", "am" and "are" are</p>
<ul>
<li>
<input type="radio" name="answer1" value="Present tense" />
<label>Present tense</label>
</li>
<li>
<input type="radio" name="answer1" value="Past tense" />
<label>Past tense</label>
</li>
<li>
<input type="radio" name="answer1" value="Future tense" />
<label>Future tense</label>
</li>
</ul>
</div>
<div id="ques2" class="ques">
<h2>Question</h2>
<p>He
<input type="text" name="answer2"/> a policeman.
</p>
</div>
Check answer!
<script src="JQ.js"></script>
<script src="function.js"></script>
<body onload="f1()" >
<form id="form1" runat="server">
<div>
<table width="100%" align="center">
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td>
<div id="starttime"></div>
<div id="endtime"></div>
<div id="showtime"></div>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</head>
</html>
Your code is good enough for a beginner but it requires some improvements.
<script type="text/javascript" >//language ="javascript" is obsolete
//var tim; //no need at all
//var min = 0; //no need at all
//var sec = 30; //there is better way
//var f = new Date(); //no need to be global
function f1(sec) {//define (declare) sec as parameter
f2(); //call the function
var f = new Date();
document.getElementById("starttime").innerHTML = "Your started your quiz at " + f.getHours() + ":" + f.getMinutes();
var showtime = document.getElementById("showtime"); //used many times
//Here we put (closure) f2
function f2() {
//f2 knows sec from parent scope
if (sec <= 0) {//parseInt(sec) no need. sec is int
showtime.innerHTML = 'Time is over';
//ShowAnswers(); //show on the same page or post to .php
return;
}
sec--;// = parseInt(sec) - 1;
showtime.innerHTML = "Your Left Time is :" + Math.floor(sec / 60) +" Minutes ," + (sec % 60) +" Seconds";
setTimeout(f2, 1000);//"f2()" is correct but this way is better
/* no need in remaining code
}
else {
if (parseInt(sec) == 0) {
min = parseInt(min) - 1;
if (parseInt(min) == 0) {
clearTimeout(tim);
location.href = "www.rawlanguages.com";
}
else {
sec = 60;
document.getElementById("showtime").innerHTML = "Your Left Time is :" + min + " Minutes ," + sec + " Seconds";
tim = setTimeout("f2()", 1000);
}
}
}
*/
}//f2
}//f1
</script>
<body onload="f1(90)"><!--Here we send seconds to the function -->
Also note that all your quiz starting from <h1>P.1... must be inside body container.

Where can I put this Javascript that references a field that hasn't been built yet?

I have a piece of JavaScript that clears out a label (well, it sets its innertext value). The problem is, after I've clicked a button and the page posts back, the label doesn't exist when the Javascript launches. So, I get an error:
0x800a138f - JavaScript runtime error: Unable to set property
'innerText' of undefined or null reference
And the code in the debug window looks like this:
.
.
<body id="PageBody" bgcolor="lightgrey">
<script type = "text/javascript">
/* Stop, Clear and Pause the timer displayed under the pause buttons */
var h1 = document.getElementsByTagName('h1')[0],
start = document.getElementById('start'),
stop = document.getElementById('stop'),
clear = document.getElementById('clear'),
seconds = 0, minutes = 0, hours = 0,
t;
function add() {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
}
}
document.getElementById('Call_Tab_TabPanelCall_h1').innerText = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);;
timer();
}
function timer() {
t = setTimeout(add, 1000);
}
function stp() {
clearTimeout(t);
}
function clr() {
document.getElementById('Call_Tab_TabPanelCall_h1').innerText = "00:00:00";
seconds = 0; minutes = 0; hours = 0;
}
</script>
<form method="post" action="./MedIntakeMain.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="ctl02_HiddenField" id="ctl02_HiddenField" value="" />
<input type="hidden" name="Call_Tab_ClientState" id="Call_Tab_ClientState" value="{"ActiveTabIndex":0,"TabEnabledState":[true,true],"TabWasLoadedOnceState":[true,false]}" />
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="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V6ohA9FiEEdFFHbuGESVTu4DrEYQD8UeDoWquN0I4yYuiEf9+6EJ0UIZPVF/UIBKvfHyb9SmKL3Dwkx2hVH1LYneM/MFZMDBcD4vmecMYoVNoeO//tuqzFa4/aqcXGoPQ/dEanFOqs5wUI2MrEJ7iRKfXKXTdcRirCCh09KXCgEqZL3exJir2rRvM=" />
</div>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
<script src="/WebResource.axd?d=pynGkmcFUV13He1Qd6_TZKuxqzo1qNK0jLS5vOjXgYcG0yOhGAUyRMkYR1c88uLjzDmFYO_wTSY6btwoEa6chQ2&t=635803038500000000" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
stp();clr();timer();//]]>
</
It just stops right at the </, which I guess is where the HTML would start? It's that first line in function clr that gets highlighted. If I comment out the 2 lines in function clr, everything works fine.
These functions are called using this asp on the front end:
<asp:Button ID="CallStart_Btn" runat="server" AccessKey="S" OnClick="CallStart_Btn_Click" Text="SĖ˛tart" Width="112px" OnClientClick="this.disabled=true; stp(); clr(); timer();" UseSubmitBehavior="False" style="margin-left: 0px"/>
And then in the code-behind I've got 3 lines like this:
ScriptManager.RegisterClientScriptBlock(UpdatePanel4, this.GetType(), "stopscript", "stp();", true);
ScriptManager.RegisterClientScriptBlock(UpdatePanel4, this.GetType(), "clearscript", "clr();", true);
ScriptManager.RegisterClientScriptBlock(UpdatePanel4, this.GetType(), "timerscript", "timer();", true);
So, where can I put that JavaScript so it will get called after the label exists?
Use
ScriptManager.RegisterStartupScript
instead of
ScriptManager.RegisterClientScriptBlock
This will put function calls at the bottom of the page.
There are a couple things at play.
You could disable/hide the button until after the label has been created (presuming some user action causes the label to appear, it could also cause the button to enable/appear).
You could add a check to each function to ensure the element exists. Something like:
function clr() {
var label = document.getElementById('Call_Tab_TabPanelCall_h1');
if(label){
label.innerText = "00:00:00";
}
seconds = 0; minutes = 0; hours = 0;
}

Append timers using jQuery

I have created a timer using jQuery and javascript. I have appended the timer in a page. Each timer has start, stop and reset button. The problem is that when i click on the second, third, fourth.......... etc timer, only the first timer is working. No other timer is working for me.Below is my code. Can anyone please help me.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<script>
window.onload=function(){document.getElementById('time').innerHTML = "00" + ":" +"00" + ":" + "00";}
</script>
<div class="timers" id="act">
<div id="time"></div>
<div class="timer_controls">
<input class="btnStart" id="start" type="button" value="Start" onclick="timer()" />
<input class="btnreset" id="reset" type="button" value="Reset" onclick="reset()" />
<input class="btnStop" id="stop" type="button" value="Stop" onclick="stopper()" />
</div>
</div>
<input id="appends" type="button" value="append" />
<div class="container"></div>
<style type="text/css">
#time{
font-size:50pt;
}
.container{
float:left;
width:100%;
height:1000px;
}
</style>
<script type="text/javascript">
i = 0;
var w;
function timer() {
if (i > 3599) {
var H = Math.floor(i / 3600);
}
else {
var H = 0;
}
var M = i - (H * 3600)
if (M > 59) {
M = Math.floor(M / 60)
}
else {
M = 0
}
var S = i - (M * 60)
if (S > 3599) {
S = Math.floor(M / 3600)
}
if (H < 10) {
H = "0" + H;
}
if (M < 10) {
M = "0" + M;
}
if (S < 10) {
S = "0" + S;
}
document.getElementById('time').innerHTML = H + ":" + M + ":" + S;
w=setTimeout('timer()', 1000);
i++;
}
function stopper(){
clearTimeout(w);
}
function reset() {
i=0;
document.getElementById('time').innerHTML = "00" + ":" +"00" + ":" + "00";
clearTimeout(w);
}
$("#appends").click(function() {
var index = $('.timers').length;
clearTimeout(w);
var timing=$('.timers').html();
$('.container').append('<div class="timers" id="act_'+(index+1)+'">'+timing+'<input class="btnStart" id="start'+(index+1)+'" type="button" value="Start" onclick="timer()" />'+'</div>');
});
</script>
</body>
</html>
You can have only one uniqe id per HTML element on page. Every time you append new timer they all have similar Id's. So you need to change some logic of your script

Categories

Resources