tl;dr : I want to refresh time inside the popover without having to toggle it again (by clicking). When it's opened, it update the time.
I'm trying to make my popover refresh but I can't find a way to do it. I have dynamic hours in it, it needs to change every seconds. It is the only content that will need to be refresh even when the page is active. The other will update if we toggle it, if I need to update it too. I tried to search many ways to do it but couldn't find any clues.
<div class="col-md-8" style="padding-top:50px;position: relative;">
<canvas class="canvas" id="canvas" width="650px" height="450px">Votre
navigateur ne supporte pas le canevas.
</canvas>
<input type="image" src="images/plane.png" id="planeimg" data-container="body" data-toggle="popover" data-placement="top"
title="Données du trajet" data-html="true"/>
<!-- POPOVER CONTENT -->
<div id="popover-content" class="hide">
<span>Départ : </span> <span id="departPopover">Montreal</span> <br/>
<span>Arrivée : </span> <span id="arrivePopover">Toronto</span> <br/>
<span>Prochaine ville : </span> <span id="nextCity">Toronto</span> <br/>
<span>Vitesse : 500 km/h</span> <br/>
<span>Temps restant : </span> <span id="timeLeft">2</span> <span>h</span> <br/>
<span>Heure : </span> <span id="timeHours"></span>
</div>
</div>
Here is my Javascript code :
$(function(){
// Enables popover
$("[data-toggle=popover]").popover({
html: true,
content: function() {
return $('#popover-content').html();
}
});
});
// TIME FUNCTION
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('timeHours').innerHTML =
h + ":" + m + ":" + s;
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
Thanks for the help guys!
Here's simple solution for updating clock:
function startTime() {
var currentTime = new Date();
var h = currentTime.getHours();
var m = currentTime.getMinutes();
var s = currentTime.getSeconds();
// Pad the minutes and seconds with leading zeros
m = ( m < 10 ? "0" : "") + m;
s = ( s < 10 ? "0" : "") + s;
// AM or PM (for 24h delete this)
var time = (h < 12) ? "AM" : "PM";
// 12 hour format (for 24h delete this)
h = (h > 12) ? h - 12 : h;
// Conver an hours form 0 to 12
h = (h == 0) ? 12 : h;
// Visual compose
var display = h + ":" + m + ":" + s + " " + time;
// (for 24h )
// var display = h + ":" + m + ":" + s;
$("#timeHours").html(display);
}
$(document).ready(function(){
setInterval('startTime()', 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="timeHours"></span>
Hope it helps!
Related
I know how to make the button appear if the time is hourly:
if ((hr == 7) || (hr == 8) || (hr == 9)) {
displaybutton.style.visibility = "visible";
}
But I'm not sure how to do it if I want to set it up at 7:30am to 9:30am instead of 7am to 9am.
window.onload = function(){
DisplayCurrentTime();
};
function DisplayCurrentTime() {
var date = new Date();
var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
var ampm = (hours >= 12) ? "PM" : "AM";
time = hours + ":" + minutes + ":" + seconds;
if(hours>=7 && hours<10 && ampm=="AM")
{
if(minutes<=30)
{
var lblTime = document.getElementById("time");
lblTime.innerHTML = time;
$("#idbuttontoshow").css({"display":"block"});
}
}
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="two"></div>
<br>
<div id="time"></div>
<div id="idbuttontoshow" style="display:none;">
<button>one</button>
<button>two</button>
<button>three</button>
</div>
This is a JavaScript showing time hour, minute and seconds now seconds are ticking minutes change it's not static i have problem.How to implement it in HTML page ?
<script type="text/javascript">
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML = h + ":" + m + ":" + s;
t = setTimeout('startTime()', 500);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
</script>
Edit your body tag to
<body onload="startTime()">
<span id="txt">10:26:07</span>
Than add bellow code where you want clock to appear
Just do it:
<span id="txt"></span>
<script type="text/javascript">
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML = h + ":" + m + ":" + s;
t = setTimeout('startTime()', 500);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
startTime();
</script>
You need an element on your page with ID txt (see your script.)
You need to start/call your function execution startTime();
In order to allow the JS DOM parser to find your #txt place your <script> before the closing </body> tag.
<div id="txt"></div>
<script type="text/javascript">
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML = h + ":" + m + ":" + s;
t = setTimeout('startTime()', 500);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
startTime(); // Execute
</script>
There could be so many version of HTML Markup for your Javascript function:
One of them could be like:
<div id="txt">
</div>
<button type="button" name="click" onclick="startTime()">Click Me!</button>
DEMO FIDDLE
need to create a log of messages where the message be saved along with the time.
I have this code, but the acresentar a new message is deleted the old and replaced by the new.
var textons = prompt("Digite sua mensagem para a ONS", "");
var areaons = prompt("Digite a area onde o problema ocorreu", "");
var date = new Date();
var d = date.getDate();
var day = (d < 10) ? '0' + d : d;
var mes = date.getMonth() + 1;
var month = (mes < 10) ? '0' + mes : mes;
var yy = date.getYear();
var year = (yy > 100) ? yy - 100 : yy;
var hours = date.getHours();
var min = date.getMinutes();
var minutes = (min < 10) ? '0' + min : min;
var sec = date.getSeconds();
var seconds = (sec < 10) ? '0' + sec : sec;
if (areaons != null && textons != null) {
document.getElementById("logdescricao").innerHTML =
"ONS: " + textons;
document.getElementById("logarea").innerHTML =
areaons;
document.getElementById("loghoracos").innerHTML =
day + "/" + month + "/" + year + " " + hours + ":" + minutes + ":" + seconds;
document.getElementById("loghoralocal").innerHTML =
day + "/" + month + "/" + year + " " + hours + ":" + minutes + ":" + seconds;
}
}
I thought about using the .append (function) but did not succeed.
how can I make the text stop being rewritten and pass to get saved?
thank
If I've understood you correctly, you just want it to add to each section in stead of overwriting?
Then all you have to do is "+=" instead of "="
document.getElementById("logdescricao").innerHTML += "ONS: " + textons
However, this is probably not the best way of doing this.
You could try:
var logdescricaoText = document.createTextNode("ONS: "+textons);
document.getElementById("logdescricao").appendChild(logdescricaoText);
But, really, if you want to make a list of things, then you should be using a table. This will allow for much neater and structured styling.
In your HTML:
<table>
<thead>
<tr>
<td>Descricao</td>
<td>LogMessage</td>
<!-- other field headers like 'time' -->
</tr>
</thead>
<tbody id=logs>
</tbody>
</table>
Then you can use the javascript (inside your 'if' block, instead of all your ".innerHTML =" stuff)
var logLine = document.createElement("tr");
document.getElementById("logs").appendChild(logLine);
var descricao = document.createElement("td");
descricao.appendChild(document.createTextNode("ONS: "+textons));
logLine.appendChild(descricao);
var logArea = document.createElement("td");
logArea.appendChild(document.createTextNode(areaons);
logLine.appendChild(logArea);
etc..
Additionally, you might want to apply a CSS styling to the 'thead' part of the HTML
eg. in your CSS file:
thead {
font-weight: bold;
}
I have the following javascript that prints the timestamp:
<script type="text/javascript">
<!--
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds = currentTime.getSeconds()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(hours + "" + minutes + seconds + month + "" + day + "" + year)
//-->
</script>
However I want to use this timestamp in many places in the page, how can i call it like $timestamp so i can control where its placed?
Thanks in advance.
Set a variable, like:
var timestamp = hours + "" + minutes + seconds + month + "" + day + "" + year;
and later in code use that variable to show info in your page, like:
var container = document.getElementById('container1');
container.innerHTML = timestamp;
where 'container1' is a html element like span, div, p, etc. ex:
<span id="container1"></span>
answer
<script>
function startTime()
{
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
<span id="txt"></span>
<script type="text/javascript">
startTime().swap('txt');
</script>
So I followed a pretty strait-forward video tutorial on adding a clock in your webpage through JS. I have the exact same code, but it's not working on mine. Any suggestions? Thank you!
This is my code:
<body>
<div id="clockDisplay">00:00</div>
<!-- JAVASCRIPT starts here -------------------------------------------------------->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" language="JavaScript">
$(window).load(function renderTime() {
var currentTime = new Date () ;
var diem = "AM" ;
var h = currentTime.getHours() ;
var m = currentTime.getminutes() ;
var s = currentTime.getSeconds() ;
if (h == 0) {
h = 12;
} else if (h > 12) {
h = h -12;
diem = "PM" ;
}
if (h < 10) {
h = "0" + h;
}
if (m < 10) {
m = "0" + m;
}
if (s < 10) {
s = "0" + s;
}
var myClock = document.getElementyID('clockDisplay');
myClock.textContent = h + ":" + m + ":" + s + " " + diem;
setTimeout(renderTime()' ,1000) ;
};
renderTime() ;
</script>
<!-- JAVASCRIPT ends here --------------------------------------------------------->
</body>
You have a syntax error (quote mismatch) in your setTimeout code. You should never use a string as the first parameter of setTimeout.
setTimeout(renderTime, 1000);
And you don't need the $(window).load() if you put your Javascript code after the element with id="clockDisplay"
function renderTime() {
...
}
renderTime();
These need to be changed as well.
getElementById()
getMinutes()
Don't know what tutorial your following but I would change this line:
myClock.textContent = h + ":" + m + ":" + s + " " + diem;
to this:
myClock.innerHTML = h + ":" + m + ":" + s + " " + diem;
Too many things to fix here, this is my code :
<body>
<div id="clockDisplay">00:00</div>
<!-- JAVASCRIPT starts here -------------------------------------------------------->
<script type="text/javascript" language="JavaScript">
function renderTime() {
var currentTime = new Date () ;
var diem = "AM" ;
var h = currentTime.getHours() ;
var m = currentTime.getMinutes() ;
var s = currentTime.getSeconds() ;
if (h == 0) {
h = 12;
} else if (h > 12) {
h = h -12;
diem = "PM" ;
}
if (h < 10) {
h = "0" + h;
}
if (m < 10) {
m = "0" + m;
}
if (s < 10) {
s = "0" + s;
}
var myClock = document.getElementById('clockDisplay');
myClock.textContent = h + ":" + m + ":" + s + " " + diem;
}
window.onload = renderTime;
setInterval(renderTime ,1000) ;
</script>
<!-- JAVASCRIPT ends here --------------------------------------------------------->
</body>
To see details fix, go there : Fixed issues detail link
I have created a digital clock on my personal developing website but its not animated...
My javascript is below:
07:23:45 PM
<script>
function webClock() {
var pos = "PM";
var pickTime = new Date();
var h = pickTime.getHours();
var m = pickTime.getMinutes();
var s = pickTime.getSeconds();
if(h == 0){
h = 12;
}else if(h>12){
h = h-12;
pos="AM";
}
if(h<10){
h = "0" + h;
}
if(m<10){
m = "0" + m;
}
if(s<10){
s = "0" + s;
}
document.getElementById('MyClock').innerHTML= h + ":" + m + ":" + s + " " +"pos";
setTimeout(webClock, 500);
}
webClock();
}
</script>