I have a function called startTime() which gets the current time with h set to the hour and m set to minutes. The error with this code is after I exported my javaScript side function to node with the specific variables when I call back h and m their values don't get updated with the startTime function they are stuck to the values they first had when I run startTime in the javascript side. I tried to setInterval to keep on calling startTime every 500 milliseconds but that didn't fix it.function refreshStartTime() {setInterval(startTime, 500);} .I also tried debugging it with an alert on the HTML side which printed the correct/constantly updating values of h and m.```function testAlert(){alert(h + ":" + m)} .Do you have any idea why it's not updating on the node side or how to solve this issue? Thanks in advance.
JavaScript side
function startTime() {
var today = new Date();
globalThis.h = today.getHours();
m = today.getMinutes();
var s = today.getSeconds();
globalThis.m = checkTime(m);
s = checkTime(s);
var t = setTimeout(startTimeAudio, 500);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
startTime()
module.exports = {
startTime,
h,
m,
};
Node side
const qw = require('./index.js');
const timeH = qw.h;
const timeM = qw.m;
Change your module.exports to
module.exports = {
startTime,
...globalThis
}
Related
This is my countdown javascript code everyday on 23:00 of local time will finish and after 1 hour again will start to countdown for tomorrow and this continues. I wanted to know is it possible to add PHP code to this till after countdown finished everyday It adds "5" to my "Credit" column ("Credit" + 5 ) in my MySQL automatically ? appreciate for any kind of guidance.
<script>
var countDown = (function() {
var startStream;
var endStream;
var streamingText = 'Your match started!';
var updateElement;
// Pad single digit numbers
function pad(n) {
return (n<10?'0':'') + +n;
}
// Format a time difference as hh:mm:ss
// d0 and d1 are date objects, d0 < d1
function timeDiff(d0, d1) {
var diff = d1 - d0;
return pad(diff/3.6e6|0) + ':' + pad((diff%3.6e6)/6e4|0) + ':' + pad(diff%6e4/1000|0);
}
// start, end are UNIX UTC time values in seconds for the start and end of streaming
return function(elementId, start, end) {
var now = new Date();
var returnValue;
// By default, run again just after next full second
var delay = 1020 - now.getMilliseconds();
// turn start and end times into local Date objects
if (start) startStream = new Date(start*1000);
if (end) endStream = new Date(end*1000);
// If now is after endStream, add 1 day,
// Use UTC to avoid daylight saving adjustments
if (now > endStream) {
endStream.setUTCHours(endStream.getUTCHours() + 24);
startStream.setUTCHours(startStream.getUTCHours() + 24);
}
// Store the element to write the text to
if (elementId) updateElement = document.getElementById(elementId);
// If it's streaming time, return streaming text
if (now >= startStream && now < endStream) {
returnValue = streamingText;
// Run again after streaming end time
delay = endStream - now;
} else {
// Otherwise, count down to startStream
returnValue = timeDiff(now, startStream);
}
// Write the time left or streaming text
updateElement.innerHTML = returnValue;
// Call again when appropriate
setTimeout(countDown, delay);
};
}());
// Testing code
// Create dates for a local time of 21:00 today
var myStart = new Date();
myStart.setHours(23,0,0,0);
var myEnd = new Date()
myEnd.setHours(24,0,0,0);
// Create UNIX time values for same time as UTC
var startUTCTimeValue = myStart/1000|0
var endUTCTimeValue = myEnd/1000|0
// Run when page loads
window.onload = function() {
countDown('foo', startUTCTimeValue, endUTCTimeValue);
}
</script>
<font face="Trebuchet MS">
<div id="foo" style="color: white; font-size: 14px; font-weight: bold;"></div>
Jquery Ajax is the way to go here i guess.
It seems like you are very new and might not know what ajax is.
In short with Ajax you can call a webpage url in the background. So You could call www.yourdomain.com/addCredits.php
It will do the same as if you would visit that url in your browser. Just write your SQL Code inside that addCredits.php File and you're done.
Just add something like this to your javascript:
$.ajax({url: "addCredits.php"});
You will have to embed jquery inside your document first though.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
And also you might want to wait for that script to load before you call that function:
$( document ).ready(function() {
$.ajax({url: "addCredits.php"});
});
Edit:
Your Counter acutally seems to continue in a loop since it keeps up calling itself in the end.
But i would say just put it where your setTimeout is at the end of your function.
Edit2:
Here is an Example with a Clock, your code seems to be over-complicated to be honest. It doesn't really work when i put it in a fiddle aswell...
You could just use this instead:
function startTime() {
// set time variables h=hour, m=minute, s=second
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
//check if 0's have to be added for better appearance. no logical use!
m = checkTime(m);
s = checkTime(s);
//display current time on the element with id="txt"
document.getElementById('txt').innerHTML =
h + ":" + m + ":" + s;
//check if its 23:00:00 ... if so call addCredits.php
if(h == 23 && m == 00 && s == 00) {
$.ajax({url: "addCredits.php"});
}
//restart this function every second to update the clock
var t = setTimeout(startTime, 1000);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>
I know this isn't going to help you understand so I'll break it down.
I have a webpage that has an interval for the time, this time is translated into an hour and that loads a audio file. The problem being that the interval reloads the audio every second and I only want it to interval based on the hour and the clock to work as intended. I have it set up like this.
The basic of it is that I have a page loading stuff based on the time of day and I need a way to edit the time live with some function and change the elements. If there is a better way to do this, I am open to suggestions.
note: i can not use jquery
//interval//
var myVar = setInterval(function(){ myTimer() }, 1000);
//get the time//
function myTimer() {
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
var time = h + ":" + m + ":" + s;
document.getElementById('time').innerHTML = time;
mod_time(h,m,s)
}
// mod the time //
function mod_time(h,m,s) {
var x = 0;
if(x == 1){
var h = h - h + 2
}
if(x == 2){
var h = h - h + 3
}
else{
var h = h;
}
//applying just the hours//
apply_mod_time(h)
//displaying the full modded time for debugging//
var t_str = h + ":" + m + ":" + s;
document.getElementById('time2').innerHTML = t_str;
}
//playing sound based on the hour//
function apply_mod_time(h) {
if (h==1) {
document.getElementById("myAudio").src = "moo.mp3";
}
}
Add another variable that specifies the current hour. When you execute the interval every second, see if h !== hourVariable. If they do not match, load the audio file and update the hourVariable to equal h.
First time using moment.js and I am struggling to implement a clock to show the time in CET and PST. My code is as follows:
function cetClock() {
var cet = moment.tz("Europe/London");
var today = new Date(cet);
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkCetTime(m);
s = checkCetTime(s);
$rootScope.cetTime = h + ":" + m + ":" + s;
var t = setTimeout(cetClock, 300);
}
function checkCetTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
cetClock()
<div class="col-md-6">
<p>CET: {{$root.cetTime}}</p>
</div>
The issue I have is that the time in the view is only being updated every 4-5 seconds. If I log the h, m, s within the function, it shows every 500 milliseconds the time being updated.
Question
Why is the clock in the viiew failing to update every second?
I suggest using $timeout instead of setTimeout which will automatically trigger a digest cycle.
I need some advice and logic in my problem.
So, I have an entrydate, from database, then the running current date, and a value of 10(double type in database). So, I know how to calculate the diff of the entrydate and current date, right. So I convert it to seconds then to a number(9.23165).
|Entry |Current Date|Diff(in number)|
|2:00:00 PM |2:30:00 PM | 5.00(Sample)|(First User)
So basically, as current date goes on, can PHP show the deduction on real time? Or I need to refresh? What I need is for it to display the deduction without refreshing. So basically, I need to know what I have to do. Maybe javascipt and ajax?
What you would need are a few Javascript/jQuery functions to update the browser in real time.
var myTimer;
var startTime;
function startTimer() {
stopTimer(); // Reset
startTime = new Date(); // Save to calculate difference
myTimer = setInterval(clockTicking, 1000);
}
function stopTimer() {
clearInterval(myTimer);
}
function clockTicking() {
var now = new Date();
var timeDiff = new Date(now - startTime); // constructor uses UTC, so use UTC date functions from here on
var hours = (timeDiff.getUTCHours() < 10) ? '0' + timeDiff.getUTCHours() : timeDiff.getUTCHours();
var mins = (timeDiff.getUTCMinutes() < 10) ? '0' + timeDiff.getUTCMinutes() : timeDiff.getUTCMinutes();
var secs = (timeDiff.getUTCSeconds() < 10) ? '0' + timeDiff.getUTCSeconds() : timeDiff.getUTCSeconds();
$("<element-where-you-display>").html(hours + ':' + mins + ':' + secs);
}
In Javascript you can call startTimer() to kick it off.
Below are the controller and view of my application from controller m passing the difference of start time and end time in duration and I want to access this duration to view page so i can assign duration value to `javascript timer and start the clock I have done using viewbag but i m getting null value i m working on online examination and i want to start a timer like countdown time when user click on start
in it can anybody help I m creating a timer control by taking the difference between two times in mvc
public ActionResult ApplyForTest(int Test_Id)
{
EAssessmentNew.BAL.StudentBal stuBal = new EAssessmentNew.BAL.StudentBal();
ViewBag.TestId = Test_Id;
DataTable testSchedule = stuBal.getTestDateTime(Test_Id);
ViewBag.Sysdate = DateTime.Now.ToShortDateString();
ViewBag.SysTime = DateTime.Now.ToShortTimeString();
for (int i = 0; i < testSchedule.Rows.Count; i++)
{
Test Date_Time = new Test();
ViewBag.Test_Date = testSchedule.Rows[i]["Test_Date"].ToString();
ViewBag.Start_Time = testSchedule.Rows[i]["Start_Time"].ToString();
ViewBag.End_Time = testSchedule.Rows[i]["End_Time"].ToString();
DateTime strtTime = DateTime.Parse(ViewBag.Start_Time);
DateTime endTime = DateTime.Parse(ViewBag.End_Time);
TimeSpan duration = (endTime - strtTime);
ViewBag.a = duration;
}
return View();
}
#model List<EAssessmentNew.Models.Question>
#{
ViewBag.Title = "TestStarted";
Layout = "~/Views/StudentMaster.cshtml";
TimeSpan t = ViewBag.a;
}
<script src="~/Scripts/jquery.js"></script>
<script src="~/Scripts/jquery-1.5.1.js"></script>
<script type="text/javascript">
var myVar = setInterval(myTimer, 1000);
var d = '#ViewBag.duration';
alert(d);
var testDate = '#ViewBag.Test_Date';
var startTime = '#ViewBag.Start_Time';
var EndTime = '#ViewBag.End_Time';
d.setHours(00, d, 59, 00);
function myTimer() {
if (d.getSeconds() <= 1)
d.setMinutes(d.getMinutes() - 1, 59, 00);
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds() - 1;
if (m == 0 && s == 1)
window.location.href = "showresults.aspx";
document.getElementById("lblCtime").innerHTML = h + ":" + m + ":"s;
d.setMinutes(m, s);
//setInterval(myTimer, 1000);
}
</script>
Above mention are the controller and view of my application from controller m passing the difference of start time and end time in duration and i want to access this duration to view page so i can assign duration value to `javascript timer and start the clock i hv done using viewbag but i m getting null value i m working on online examination and i want to start a timer like countdown time when user click on start
in it can anybody help I m creating a timer control by taking the difference between two times in mvc
I have used all the required javascript but m getting null value on viewpage viewbag
I would seriously take a look at SignalR for this instead of doing this in javascript. You can have your timer run on the server tied to a particular web connection to the SignalR hub and then have the SignalR Hub broadcast to your javascript method the running clock.