Count down timer which don't reset on page refresh - jsf

How can I create a count down timer in JSF? I don't want the timer reset when refresh the page.

You can simply use Java Script for it.
Or
You can use RichFaces a4j:poll and poll count data from server where your timer will be running

You can use simply this script
var seconds = readCookie('countdown') || 60;
alert(seconds);
function secondPassed()
{
seconds--;
var minutes = parseInt(seconds / 60);
var remainingSeconds = parseInt(seconds % 60);
if (remainingSeconds < 10) {
remainingSeconds = "0" + parseInt(remainingSeconds);
}
document.getElementById('countdown').innerHTML = minutes + " mins : " + remainingSeconds + " secs";
if (parseInt(seconds) === 0)
{
alert("Time is over");
clearInterval(countdownTimer);
document.getElementById('countdown').innerHTML = "Time is Over";
document.forms["myForm"].submit();
document.location.href = "examresult.jsp";
}
}
var countdownTimer = setInterval(function() {
secondPassed();
if (seconds === 0) {
eraseCookie(seconds);
} else {
createCookie(seconds, seconds, 7);
}
}, 1000);

Related

Variable is undefined outside of the function

Can someone please help me out with this? I'm getting an error 'count is not defined' when executing the below code. I'm trying to create a countdown clock in node.js. If I log the timer variable inside the function it works but outside of the function, it is undefined.
const getOrder = async(request, response) => {
const data = await knex("orderbook_table").select()
const time_data = await knex("orderbook_table").select("timestamp")
console.log(time_data)
var timer;
setInterval(function count() {
for (var i = 0; i < time_data.length; i++) {
//console.log(tableRows[i]);
var entryDate = time_data[i].timestamp;
const second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24;
countDown = new Date(entryDate).getTime();
let now = new Date().getTime();
distance = countDown - now;
days = Math.floor(distance / (day));
hours = Math.floor((distance % (day)) / (hour));
minutes = Math.floor((distance % (hour)) / (minute));
seconds = Math.floor((distance % (minute)) / second);
timer = days + "d:" + hours + "h:" + minutes + "m:" + seconds + "s";
//console.log(timer);
}
}, 1000);
var orderStr = data;
count();
console.log(timer);
response.send({ orderStr });
//refresh()
The count function is defined and scoped under the setTimer, you should create the function as callback outside of setInterval then call within that setInterval the created function
You can give a look to this previously answered thread or to the documentation

How to calculate the working hours in flutter using mongodb

I want to calculate the working hours, my scenario is, when user login to the app and clicks on time in button that time will be stored in the database (I am using mongodb) and if user logout and then login again and then time in button should display the time at which user did last time, and then if user clicks on time out button then it should calculate the working hours, for example, if user time in at 9:10:35 AM and time out at 5:50:47 PM then the total working hours should be 9:40:13 , but from my logic of calculating total working hours it give me this 4:40:12.
Here is my code:
totalWorkingHours() {
print("working hours");
var totalWorkingMin;
var totalWorkingSec;
var totalWorkingHour;
var timeInHoursSplit;
var timeOutHoursSplit;
var timeInHours0;
var timeOutHours0;
List timeInHours;
List timeOutHours;
var inHour;
var outHour;
var inMin;
var outMin;
var inSec;
var outSec;
var flagMin = false;
var flagSec = false;
var defaultIn = " 00:00:00 PM";
var defaultOut = " 00:00:00 PM";
var data = {};
//here i am getting the time in and time out values and using api if in case user logout, and spliting it to get the hours, min and sec
if(workinghours=="0" && getapitimeout==""){
print("11");
timeInHoursSplit = defaultIn.split(' '); //18:35:00 PM
timeOutHoursSplit = defaultOut.split(' ');
}
else if (workinghours == "0" && getTimeInStatus == true) {
print("12");
timeInHoursSplit = gettimeinvalue.split(' '); //18:35:00 PM
timeOutHoursSplit = getapitimeout.split(' ');
}
//if logged in , did timein and timeout at once.
else if(gettimeinworkinghours=="0" && getapitimeout!=""){
print("13");
print(getapitimein);
print(getapitimeout);
timeInHoursSplit = getapitimein.split(' ');
timeOutHoursSplit = getapitimeout.split(' ');
}
//if not time in and time out yet
else {
print("14");
print(getapitimeout);
timeInHoursSplit = defaultIn.split(' ');
timeOutHoursSplit = defaultOut.split(' ');
}
print("1");
print(timeInHoursSplit); //[18:35:00, PM]
timeInHours0 = timeInHoursSplit[1]; //18:35:00
print("2");
print(timeInHours0);
timeOutHours0 = timeOutHoursSplit[1];
timeInHours = timeInHours0.split(':'); //[18, 35, 00]
print(timeInHours);
timeOutHours = timeOutHours0.split(':');
inHour = timeInHours[0];
outHour = timeOutHours[0];
inMin = timeInHours[1];
outMin = timeOutHours[1];
inSec=timeInHours[2];
outSec=timeOutHours[2];
// here i am calculating the hours
if (int.parse(inHour) > int.parse(outHour)) {
totalWorkingHour = int.parse(inHour) - int.parse(outHour);
print(totalWorkingHours);
} else {
totalWorkingHour = int.parse(outHour) - int.parse(outHour);
print(totalWorkingHours);
}
// here i am calculating the min
if (inMin != outMin) {
if (int.parse(outMin) > int.parse(inMin)) {
totalWorkingMin = int.parse(outMin) - int.parse(inMin);
flagMin = true;
} else if (int.parse(inMin) > int.parse(outMin)) {
totalWorkingMin = int.parse(inMin) - int.parse(outMin);
flagMin = true;
}
} else if (inMin == outMin) {
totalWorkingMin = int.parse(outMin) - int.parse(inMin);
flagMin = true;
}
// here i am calculating the sec
if (inSec != outSec) {
if (int.parse(outSec) > int.parse(inSec)) {
totalWorkingSec = int.parse(outSec) - int.parse(inSec);
flagSec = true;
} else if (int.parse(inSec) > int.parse(outSec)) {
totalWorkingSec = int.parse(inSec) - int.parse(outSec);
flagSec = true;
}
} else if (inSec == outSec) {
totalWorkingSec = int.parse(outSec) - int.parse(inSec);
flagSec = true;
}
//// here i am getting the totalworkinghours by concatenating the hours, min and sec
if (flagMin == true && flagSec==true) {
print("1 output");
totalTime = totalWorkingHour.toString() +":" +totalWorkingMin.toString() +":"+totalWorkingSec.toString();
print(totalTime);
return totalTime;
}
//cond 2
else if (flagMin == false && flagSec==false){
print("2 output");
totalTime = totalWorkingHour.toString() +":" +inMin.toString() + ":" +inSec.toString();
return totalTime;
}
//cond 3
else if (flagMin == false && flagSec == true) {
print("3 output");
totalTime = totalWorkingHour.toString() +":" +inMin.toString() ;
return totalTime;
}
//cond 4
else if (flagMin == true && flagSec == false) {
print("4 output");
totalTime = totalWorkingHour.toString() +":" +totalWorkingMin.toString() +":" +inSec.toString();
return totalTime;
}
}
Kindly please help how I can do this.

Phaser stopwatch

I have started a game and I want a stopwatch (countup timer) in it I have found a code to activate it. But how to stop it?
Source: https://docs.idew.org/video-game/project-references/phaser-coding/timers#create-count-up-timer
Code related to the stopwatch:
//global vars
var timeText; var min, sec;
function create timeText = game.add.text(600, 20, "", { fontSize: '20px', fill: '#FFF' }); timeText.fixedToCamera = true;
function displayTimeElapsed(){
if (knight.x >= 96){
var time = Math.floor(game.time.totalElapsedSeconds() );
min = Math.floor(time / 60);
sec = time % 60;
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
timeText.text = "Time: " + min + ":" + sec;
}
}
//update displayTimeElapsed();
you could add a boolean pause that if true it will set the time to what the remaining time is and when false keep doing what is doing.
it would be easier to explain it if you show your code. I will update my answer accordingly.
Lets create it from scratch,
first of all in your create() function, lets add a text to display the timer on the screen:
// CREATE()
this.timerText = this.add.text(x, y, "").setColor("#000000");
Second lets create a function, bellow the update(), to count down :
showTimer(){
// Assuming you want 60 seconds, if not just chenge the 60's for whatever time you want
let maxTime = 60;
let time = Math.floor(this.time.totalElapsedSeconds() );
let sec = time % 60;
timeText.setText(sec); // Adding the timer to our text
}
Third, create a variable in the create() to track when the timer ends:
// CREATE()
this.timerOver = false;
// And lets start the timer
this.timer = this.time.delayedCall(60000);
Now lets modify our showTimer() function:
showTimer(){
let maxTime = 60;
let time = Math.floor(this.time.totalElapsedSeconds() );
let timeLeft = maxTime - time; // Check how much time is left
// When the countdown is over
if(timeLeft <= 0){
timeLeft = 0;
this.timerOver = true; // Setting our variable to true
}
let sec = time % 60;
timeText.setText(sec);
}
and last, in our update() function lets check if our variable this.timerOver is true
if (this.timerOver === false){
this.showTimer(); // Calling our function every frame
}
else {
// Whatever you want it to do when timer comes to 0
}

Instagram Auto-Like JavaScript BOT

This code brings back an error of
Uncaught TypeError: Cannot read property 'innerHTML' of null
at doLike (<anonymous>:20:21)
at <anonymous>:35:1
doLike # VM1269:20
(anonymous) # VM1269:35
It has worked in the past, I got it from this website : https://blog.joeldare.com/simple-instagram-like-bot/
function getHeartElement() {
var knownHeartElementNames = ["coreSpriteHeartOpen", "coreSpriteLikeHeartOpen"];
var i = 0;
// Loop through the known heart elements until one works
for (i = 0; i < knownHeartElementNames.length; i++) {
var heartElement = document.querySelector("." + knownHeartElementNames[i]);
if (heartElement != undefined) {
break;
}
}
return heartElement;
}
function doLike() {
var likeMax = 100;
var likeElement = getHeartElement();
var nextElement = document.querySelector(".coreSpriteRightPaginationArrow");
likeCount++;
var nextTime = Math.random() * (14000 - 4000) + 4000;
if (likeElement.innerHTML.match("Unlike") == null) {
likeElement.click();
console.log(likeCount + " - liked");
} else {
console.log(likeCount + " - skipped");
}
setTimeout(function() {nextElement.click();}, 1000);
if (likeCount < likeMax) {
setTimeout(doLike, nextTime);
} else {
console.log("Nice! Time for a break.");
}
}
var likeCount = 0;
doLike();
You may want to use a tool such a Keygram - https://www.thekeygram.com
It works really well for me to gain followers

Detailed cooldown for commands

I saw in some discord servers that they have a detailed cooldown and that they can exactly see how long it takes before they can use that command again
but I don't know how add this, can someone help me?
I have this now
const talkedRecently = new Set();
if (talkedRecently.has(msg.author.id)) {
msg.channel.send("Wait 1 minute before getting typing this again. - " + msg.author);
} else {
talkedRecently.add(msg.author.id);
setTimeout(() => {
talkedRecently.delete(msg.author.id);
}, 60000);
}
but here you can't see exactly how long you have to wait. I want it like this:
If you create an object of cooldowns you can get how much time they would have left by subtracting the date from the cooldown.
Like so:
//Start of code or something
var cooldowns = {}
var minute = 60000;
var hour = minute * 24;
//Set cooldown
cooldowns[message.author.id] = Date.now() + hour * 24; //Set a 24 hour cooldown
//At command check
if(cooldowns[message.author.id]){
if(cooldowns[message.author.id] > Date.now()) delete cooldowns[message.author.id];
else console.log("user still has " + Math.round((cooldowns[message.author.id] - Date.now)/minute) + " minutes left"
}
I haven't tested it, but the simplest way to do this is something like:
const talkedRecently = new Set();
const seconds = "60";
loopnum = 0
while (loopnum <= seconds)) {
loopnum = loopnum + 1;
if (talkedRecently.has(msg.author.id + "-" + loopnum)) {
msg.channel.send("Wait **" + loopnum + " **seconds before getting typing this again. - " + msg.author);
return;
}
}
loopnum = seconds;
while (loopnum = 0) {
setTimeout(() => {
if (talkedRecently.has(msg.author.id + "-" + (loopnum+1))) {
talkedRecently.delete(msg.author.id "-" + (loopnum+1));
}
talkedRecently.add(msg.author.id + "-" + loopnum);
}, 1000);
loopnum = loopnum - 1;
}
if (talkedRecently.has(msg.author.id + "-1")) {
talkedRecently.delete(msg.author.id "-1");
}
return;

Resources