Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Subtracting current hour’s value from previous hour’s value using javascript

I got my first touch with js yesterday and i got this small task to do a small script in bulding controller. I am reading values from certain location and calculating energy, and after 1 hour I should read the same values and subtract them to get get delta energy delta. The script should run constantly so Im using while(1) sleep(3,6 * 1000000)

Here is my code where I'm at

function summa() {
   var E1 = (parseFloat(read("location1","value1"))     *
   parseFloat(read("location11","value11"))) / Math.pow(10,6)
 
   executePropertyCommand("object1","Value","Write", E1) 
   
   var E2 = (parseFloat(read("location2","value2"))     *
   parseFloat(read("location22","value22"))) / Math.pow(10,6)
 
   executePropertyCommand("object2","Value","Write", E2)
 
   var  IT_Sum = (E1 + E2) 
    return IT_Sum}

 setTimeout(summa1,3.599 * 1000000);{
 function summa1() {
   var E1 = (parseFloat(read("location1","value1"))     *
   parseFloat(read("location1","value1"))) / Math.pow(10,6)
 
   var E2 = (parseFloat(read("location2","value2"))     *
   parseFloat(read("location22","value2"))) / Math.pow(10,6)
 
   var  IT_Sum1 = (E1 + E2)
   return IT_Sum1 }}

 while(1) {     
 var sum1 = summa()
 var sum2 = summa1()  
 var IT_delta = summa2 - summa1 
 sleep(3.6 * 1000000)}

I've tried to locate the settimeout in different locations like into the while loop but i cant seem to get the sum2 to wait for the delay.

Any ideas for better way to calculate the subtraction of same data in 1 hour loops?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can add values to the array every hour and then calculate the difference of adjacent indexes.

To run code every hour use window.setTimeout and pass callback and time.

// array with values added each hour
var numbers = [];
function runHourly() {
 var dateNow = new Date();
 var mins = dateNow.getMinutes();
 var secs = dateNow.getSeconds();
 var interval = (60*(60-mins)+(60-secs))*1000;
 if (interval > 0) { 
    window.setTimeout(runHourly, interval);
 }
 // your code for calculating delta goes here
 numbers.push(summa());

 if(numbers.length >= 2) {
    var IT_delta = numbers[1] - numbers[0];
    // do something with delta here
    // shift array for getting delta in an hour for new values…
    numbers.shift();
    
 }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!