如何在每个响应中将变量的先前值与新变量值进行比较 node.js
How to compare previous value of variable with new variable value on each response node.js
我是 运行 cron,它每 30 秒检查一次价格,并将 return 我的资产价值。
如果价格与之前的价格不同,我想记录下来。我正在测试这个,因为这对我未来的想法非常有用... price
变量控制着价格。我的目标是在变量不同时执行一些操作。
//Stuff above the code like cron and general discord client.on ↑
//this will log current price of asset
console.log(price, 'PRICE UPDATED!')
// tried to compare price however this is unsucessful, as it doesn't do anything...
var Previousprice = null;
function foo() {
var Currentprice = $(price.toFixed(2))
if (Previousprice == Currentprice) {
$(price.toFixed(2))
console.log('Price is same!')
}
Previousprice = Currentprice
console.log('Price has changed!')
//new discord action...
}
});
});
});
});
到目前为止我也尝试过这个,但我认为这完全没用,不会那样工作...
console.log(price, 'PRICE UPDATED!')
let Previousprice = null;
let Currentprice = price
if (price) {
let Previousprice = price.toFixed(2)
console.log(price, 'Price defined')
} else if(Previousprice === Currentprice) {
console.log(price, 'Price is same')
} else if(Previousprice !== Currentprice) {
let Previousprice = price.toFixed(2)
console.log(price, 'Price changed!')
}
Logs:
1.29739982471208 PRICE UPDATED!
1.29739982471208 Price defined
1.29739982471208 PRICE UPDATED!
1.29739982471208 Price defined
1.29660532105896 PRICE UPDATED!
1.29660532105896 Price defined
好吧,你有没有试过 previous_price = current_price 在 previous_price 当前设置为某个价格后, 运行 它,然后 console.log如果它不同或什么的,那么它会被设置为当前价格,然后它会在每次内部更改后改变?
这应该有效。
您正在重新声明和分配 previousPrice = null;每 30 秒。如果没有看到周围的代码就不能给你细节,但你通常想要的是一个像 currentPrice 这样的全局变量,当应用程序第一次 运行s 时初始化为 0。这样的事情应该有效:
// initialise a global variable with inital value of 0
let currentPrice = 0;
// Stuff above the code like cron and general discord client.on
console.log(price, 'PRICE UPDATED!');
if (price !== currentPrice) {
console.log(`Price changed from ${currentPrice} to ${price}`);
currentPrice = price;
}
else {
console.log(`Price remains at ${currentPrice}`);
}
第一个运行显然会将价格从0变为第一个初始价格,但是没关系。
我是 运行 cron,它每 30 秒检查一次价格,并将 return 我的资产价值。
如果价格与之前的价格不同,我想记录下来。我正在测试这个,因为这对我未来的想法非常有用... price
变量控制着价格。我的目标是在变量不同时执行一些操作。
//Stuff above the code like cron and general discord client.on ↑
//this will log current price of asset
console.log(price, 'PRICE UPDATED!')
// tried to compare price however this is unsucessful, as it doesn't do anything...
var Previousprice = null;
function foo() {
var Currentprice = $(price.toFixed(2))
if (Previousprice == Currentprice) {
$(price.toFixed(2))
console.log('Price is same!')
}
Previousprice = Currentprice
console.log('Price has changed!')
//new discord action...
}
});
});
});
});
到目前为止我也尝试过这个,但我认为这完全没用,不会那样工作...
console.log(price, 'PRICE UPDATED!')
let Previousprice = null;
let Currentprice = price
if (price) {
let Previousprice = price.toFixed(2)
console.log(price, 'Price defined')
} else if(Previousprice === Currentprice) {
console.log(price, 'Price is same')
} else if(Previousprice !== Currentprice) {
let Previousprice = price.toFixed(2)
console.log(price, 'Price changed!')
}
Logs:
1.29739982471208 PRICE UPDATED!
1.29739982471208 Price defined
1.29739982471208 PRICE UPDATED!
1.29739982471208 Price defined
1.29660532105896 PRICE UPDATED!
1.29660532105896 Price defined
好吧,你有没有试过 previous_price = current_price 在 previous_price 当前设置为某个价格后, 运行 它,然后 console.log如果它不同或什么的,那么它会被设置为当前价格,然后它会在每次内部更改后改变?
这应该有效。
您正在重新声明和分配 previousPrice = null;每 30 秒。如果没有看到周围的代码就不能给你细节,但你通常想要的是一个像 currentPrice 这样的全局变量,当应用程序第一次 运行s 时初始化为 0。这样的事情应该有效:
// initialise a global variable with inital value of 0
let currentPrice = 0;
// Stuff above the code like cron and general discord client.on
console.log(price, 'PRICE UPDATED!');
if (price !== currentPrice) {
console.log(`Price changed from ${currentPrice} to ${price}`);
currentPrice = price;
}
else {
console.log(`Price remains at ${currentPrice}`);
}
第一个运行显然会将价格从0变为第一个初始价格,但是没关系。