Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda