Im looking at a javascript file trying to figure out a timer issue, but im lost as to what exactly is happening here. Could someone break down this code into bite sizes and explain what is going on?
Timer=0;
function countdown(auctionid) {
var auctions;
var divs;
Timer=Timer+1;
if((Timer%10=="0")||(Timer=="1")) {
$.get("current.php", {
id:auctionid
}, function(data) {
auctions=data.split("||");
for(n=0;n<=auctions.length;n++) {
if(auctions[n] != undefined) {
divis=auctions[n].split("##");
$('#futu'+divis[0]).html(divis[1]);
}
}
});
}
var cauctionid="auctionid";
var tauctions=auctionid.split("|");
for(i=0;i<=tauctions.length;i++) {
if(tauctions[i] != undefined) {
var dd=$('#futu'+tauctions[i]).text();
var cdd=dd-1;
$('#futu'+tauctions[i]).html(cdd);
dd=dd*1000;
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor(dd/(60*60*1000)*1)
dmin=Math.floor((dd%(60*60*1000))/(60*1000)*1)
dsec=Math.floor(((dd%(60*60*1000))%(60*1000))/1000*1)
if(dday==0 && dhour==0 && dmin==0 && dsec==0) {
$('#Bid'+tauctions[i]).html("SOLD");
//return
}
if(dhour <=9) {
dhour = "0"+dhour;
}
if(dmin <=9) {
dmin = "0"+dmin;
}
if(dsec <=9) {
dsec = "0"+dsec;
}
if(dd>=1000) {
var valll=dhour+":"+dmin+":"+dsec;
}
if(dd<1000) {
var valll="00:00:00";
}
$('#Bid'+tauctions[i]).html(valll);
}
}
refreshID = setTimeout("countdown('"+auctionid+"')", 1000);
}
Every second, this script will update the time left for each of the "auctions" on the page. The second argument to setTimeout() is the time to wait in milliseconds, thus 1000 = 1 second.
Also, on the 1st second, and every 10s afterwards, it will make an AJAX call to retrieve a set of auctions in double-pipe (||) delimited string format. It then updates the corresponding auctions on the page with the data from the server.
Related
var timeToJoin = '18:13';
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes();
var SST = 'https://meet.google.com/rnc-akmx-ubk';
if (time == timeToJoin) {
joinClass();
}
function joinClass() {
location.href = SST;
setTimeout(offMicVideo, 10000);
}
function offMicVideo() {
var video = document.getElementsByClassName('I5fjHe');
var mic = document.getElementsByClassName('oTVIqe');
for (var i = 0; i < video.length; i++) {
video[i].click();
}
for (var i = 0; i < mic.length; i++) {
mic[i].click();
}
}
This is my javascript code, what it simply does is open google when the correct time comes and join my online class. A small problem is in this line if
(time == timeToJoin) {
joinClass();
}
Whats happening here is the condition I have given is true for a minute and therefore the bot keeps try to join a class for one minute, it opens the link then again opens the same link till the condition becomes false.
I tried my best to solve this problem but don't know why none of them worked.
As the script starts from scratch each time, you'll need to persist whether the videoclass was joined in a previous run. You can use Chrome's storage API for that. The documentation explains:
You must declare the "storage" permission in the extension manifest to use the storage API. For example:
{
"name": "My extension",
...
"permissions": [
"storage"
],
...
}
To store user data for your extension, you can use either storage.sync [...] of storage.local:
chrome.storage.sync.set({key: value}, function() {
console.log('Value is set to ' + value);
});
chrome.storage.sync.get(['key'], function(result) {
console.log('Value currently is ' + result.key);
});
So once you have the manifest adapted, change the following part of your code:
if (time == timeToJoin) {
joinClass();
}
...to this:
chrome.storage.sync.get({ classStarted: false }, function({classStarted}) {
if ((time === timeToJoin) === classStarted) return; // nothing to do
if (!classStarted) {
// Persist the fact that we start the class, and call joinClass once it is persisted
chrome.storage.sync.set({ classStarted: true }, joinClass);
} else {
// At least one minute elapsed, so we can clean up the persisted value now...
chrome.storage.sync.remove("classStarted");
}
});
Why is my pastPres function not working? It is supposed to change the color for a time block in a calendar depending on whether that block is in the past present or future.
$(document).ready(function () {
$(".saveBtn").on("click", function () {
var description = $(this).siblings(".description").val();
var time = $(this).parent().attr("id")
localStorage.setItem(time, description)
})
function pastPres() {
var blockTime = moment().hour()
$(".time-block").each(function () {
var time = $(this).attr("id")
if (time < blockTime) {
$(this).addClass("past")
} else if(time > blockTime) {
$(this).removeClass("past")
$(this).addClass("future")
} else {
$(this).removeClass("future")
$(this).addClass("present")
}
})
}
pastPres()
var interval = setInterval(pastPres, 15000)
$("#9am .description").val(localStorage.getItem("9am"))
$("#10am .description").val(localStorage.getItem("10am"))
$("#11am .description").val(localStorage.getItem("11am"))
$("#12pm .description").val(localStorage.getItem("12pm"))
$("#1pm .description").val(localStorage.getItem("1pm"))
$("#2pm .description").val(localStorage.getItem("2pm"))
$("#3pm .description").val(localStorage.getItem("3pm"))
$("#4pm .description").val(localStorage.getItem("4pm"))
$("#5pm .description").val(localStorage.getItem("5pm"))
$("#currentDay").text(moment().format("MMMM DD, YYYY"))
})
You are comparing strings. You need to compare time values instead. Since you are using moment you can invoke a new moment instance for the time and let it handle parsing to military time:
function pastPres() {
var blockTime = moment().hour();
$(".time-block").each(function () {
var time = moment($(this).attr("id"), ["hA"]).hour()
if (time < blockTime) {
$(this).addClass("past")
} else if(time > blockTime) {
$(this).removeClass("past")
$(this).addClass("future")
} else {
$(this).removeClass("future")
$(this).addClass("present")
}
})
}
Assuming your .time-block elements are the same as #9am, #10am, etc, it appears you're trying to compare a string to a number like
'9am' < 10
which as far as JavaScript is concerned, is false.
I suggest you add some better data to your elements like
<div id="9pm" data-hour="21" class="time-block">
and use
const time = $(this).data('hour')
what i want to achieve, is try to increase the precison of the values returned by the heart beat sensor of a Tizen smartwatch.
The values are Float64 numbers, since the language is Javascript.
I tried to use a function like this:
function strip(interval) {
return (parseFloat(interval).toPrecision(4));
}
but with no success. Maybe i'm doing something wrong, like doing some programming mistakes, i really don't know. Apparently, the IDE compile and build the package to install with no problem, but i can't see something different with or without this function included.
I will post my entire code below. Please check when is created the function strip . I've used the escamotage if (interval !== 0) {
interval_screen = interval;
} because i don't want the zeros to be printed. Please note that i want the variable streamed to the ROS topic HeartRateInterval to remain a Float; this is why i've also used the parseFloat function.
Thank you!
Code :
document.addEventListener('tizenhwkey', function(e) {
if(e.keyName === "back")
window.webapis.motion.stop("HRM");
tizen.application.getCurrentApplication().exit();
});
function Connect(){
var ip;
var connection=false;
var interval_screen = 0;
if (document.getElementById("ip").value==="")
{
ip="10.42.0.1";
}
else
{
ip=document.getElementById("ip").value;
}
var ros = new ROSLIB.Ros({
url : 'ws://' + ip +':9090'
});
ros.on('connection', function() {
connection=true;
document.getElementById("Connection_status").setAttribute("color","green");
document.getElementById("Connection_status").innerHTML = 'Connected';
tizen.power.request("SCREEN", "SCREEN_DIM");
});
ros.on('error', function(error) {
document.getElementById("Connection_status").setAttribute("color","orange");
document.getElementById("Connection_status").innerHTML = 'Error';
});
ros.on('close', function() {
document.getElementById("Connection_status").setAttribute("color","red");
document.getElementById("Connection_status").innerHTML = 'Unconnected';
connection=false;
tizen.power.release("SCREEN");
});
var RatePub = new ROSLIB.Topic({
ros : ros,
name : '/HeartRateData',
messageType : 'std_msgs/Float64'
});
var IntervalPub = new ROSLIB.Topic({
ros : ros,
name : '/HeartRateInterval',
messageType : 'std_msgs/Float64'
});
window.webapis.motion.start("HRM", onchangedCB);
function onchangedCB(hrmInfo)
{
var rate = hrmInfo.heartRate;
document.getElementById("mytext").innerHTML = 'Heart Rate= ' + rate + ' bpm';
var interval = hrmInfo.rRInterval/1000;
function strip(interval) {
return (parseFloat(interval).toPrecision(4));
}
if (interval !== 0) {
interval_screen = interval;
}
document.getElementById("mytext1").innerHTML = 'RR Interval= ' + interval_screen + ' s';
var Float64 = new ROSLIB.Message({
data:rate
});
if(connection===true)
{
RatePub.publish(Float64);
}
else
{
document.getElementById("mytext").innerHTML = 'Heart Rate = 0 bpm';
}
var Float64 = new ROSLIB.Message({
data:interval
});
if(connection===true)
{ if (interval !== 0) {
IntervalPub.publish(Float64);
}
else {
}
}
else
{
document.getElementById("mytext1").innerHTML = 'RR Interval = 0 s';
}
}}
Am I missing something here, but I can not find where you actually call that new function?
And why do you create it inline inside the onchangedCB function?
It looks as if you expected that function to be called because you declare it there and call the parameter the same as the interval variable. Which will not work (as far as I know in any programming language).
Then what I would try is call that function parseFloat(interval).toPrecision
directly instead of putting it in another function.
But what I'm far more interested in is:
here hrmInfo.rRInterval/1000
the orginal value is devived by a thousand.
Remove that division (like this var interval = hrmInfo.rRInterval;) and see if there actually are more numbers where the decimal point would be.
I can not make it up from your example, but if the value normally is something like 120 per minute. And you want to know if there are more precise values behind that, then the value should now look something like 1200054 if it is all zeroes like 120000 all the time, then the systems creating that event does not give off a more precise measure.
I am working on a bit of code in Javascript that polls a time consuming process that is running in a webservice and returns the status every two seconds. The processPoll function is never getting hit and I can not figure out why the setInterval does not work. I think I have the scope right so I'm not sure why processPoll does not start.
var processId;
var timerId;
function processStartReturn(retVal) {
if ((retVal != null) && (retVal != "")) {
processId = retVal;
timerId = setInterval(processPoll, 2000);
alert(processId); --> alerts correct Id
}
}
function processPoll() {
alert("This alert never shows up!");
WebService.MyFunction(processId, 0);
}
function startPoll() {
var appName = document.getElementById("appName").value;
var threadId = appName + "object";
processStartReturn(threadId);
}
Edit: I have added the startPoll() function that is started with an onclientclick event.
I was trying to show a text gradually on the screen (like marquee). e.g. H.. He.. Hell.. Hello. when I'm tracing it in debug in VS2010 it's working! but when it's actually running it shows the whole sentence at once.
I made a certain "delay" for about 3 seconds between each letter so it would suppose to take a while, but in reality it's shows everything immediately.
Who's the genius to solve this mystery? (please don't give me advices how to create the marquee effect, it's not the issue anymore. now it's just a WAR between me and javascript!) I'm assuming that it has to do with synchronization when calling function from function?
Thanks to whomever will help me get my sanity back.
you can download the code from here (VS project):
http://pcgroup.co.il/downloads/misc/function_from_function.zip
or view it here:
<body>
<script type="text/javascript">
//trying to display this source sentence letter by letter:
var source = "hi javascript why are you being such a pain";
var target = "";
var pos = 0;
var mayGoOn = false;
//this function calls another function which suppose to "build" the sentence increasing index using the global var pos (it's even working when following it in debug)
function textticker() {
if (pos < source.length) {
flash();
if (mayGoOn == true) {
pos++;
mayGoOn = false;
document.write(target);
textticker();
}
}
}
function flash() {
//I tried to put returns everywhere assuming that this may solve it probably one of them in not necessary but it doesn't solve it
if (mayGoOn == true) { return; }
while (true) {
var d = new Date();
if (d.getSeconds() % 3 == 0) {
//alert('this suppose to happen only in about every 3 seconds');
target = source.substring(0, pos);
mayGoOn = true;
return;
}
}
}
textticker();
</script>
You're obviously doing it wrong. Take a look at this.
var message = "Hello World!";
function print(msg, idx) {
if(!idx) {
idx = 0;
}
$('#hello').html(msg.substring(0, idx));
if(idx < msg.length) {
setTimeout(function() { print(msg, idx + 1) }, 200);
}
}
print(message);
Demo: http://jsbin.com/evehus