【发布时间】:2021-07-21 04:27:54
【问题描述】:
要温柔:
我是 Javascript 新手,我很好奇我将如何运行一个 for 循环 [或建议一个更合适的不同循环] 在循环中的条件变为 True 后重新定义“let”值。当最后一个“else if”条件变为 True 时,它将通过添加 const add_to 来重新定义 Value1 值,然后再次开始前两个条件检查。无限循环?
我在下面提供了我当前代码的示例:
const fetch = require('node-fetch');
const api_url = 'https://an-api-i-am-using-that-returns-Value1.com'
let Value1 = 100;
const add_to = 10;
async function getTotal() {
const response = await fetch(api_url);
Number_Returned = await response.json();
Number_Left = Value1 - Number_Returned
Time_Left = Number_Left * 60;
if (Number_Left > 0){
console.log(Number_Left)
var d = parseInt(Time_Left / (3600 * 24));
console.log(d)
nd = Time_Left % (24 * 3600);
var h = parseInt(nd/3600);
console.log(h)
Time_Left %= 3600;
var m = parseInt((Time_Left/60));
console.log(m)
const longEnUSFormatter = new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'long' });
var date = new Date();
date.setMinutes(date.getMinutes() + (d * 24 * 60) + (h * 60) + m);
console.log(longEnUSFormatter.format(date))
}
else if (Number_Left == 0 && Value1 == Number_Returned) {
console.log(Number_Left)
d = "no";
console.log(d)
h = "no";
console.log(h)
m = "no";
console.log(m)
date = "Today";
console.log(date)
}
else if (Number_Left < 0) {
console.log(Number_Left)
d = "didn't work";
console.log(d)
h = "didn't work";
console.log(h)
m = "didn't work";
console.log(m)
date = "didn't work";
console.log(date)
Value1 =+ add_to;
}
}
async function begin(){
console.log('calling');
const result = await getTotal();
console.log(Value1);
}
begin();
目前脚本运行后返回如下结果(示例结果):
当 (Number_Left > 0) 为真时:
calling
3
0
0
3
Tuesday, April 27, 2021 at 7:55:59 PM GMT+1
当 (Number_Left == 0 && Value1 == Number_Returned) 为真时:
calling
0
no
no
no
Today
98273492
当 (Number_Left
calling
-7
didn't work
didn't work
didn't work
didn't work
10
重申一下,一旦最后一个条件变为 True,我想将 const add_to 添加到 let 值 Value1,再次开始 (Number_Left > 0) 和 (Number_Left == 0 && Value1 == Number_Returned) 条件检查。
如果我能更具体地说明我想要完成的工作,请告诉我。提前致谢。
【问题讨论】:
标签: javascript loops for-loop conditional-statements timedelta