如何将 if / else 语句与 JSON 获取 API 响应 Javascript 一起使用
How to use if / else statement with a JSON fetch API response Javascript
我有两个工作代码片段,我想 link 具有 if
/else
逻辑。代码的第一部分从网络 API 接收 JSON 响应。 JSON.TEMPERATURE
是所需的关键变量。
const fetch = require("node-fetch");
const api_url = 'https://www.placeholder_URL.com/api/getTemperature'
fetch(api_url)
.then((response) => {
return response.json();
})
.then((myJSON) => {
console.log(myJSON.TEMPERATURE); // **This returns a number between 8 and 15**
});
代码的第二部分播放 MIDI 音符。在下面的例子中,note
# 60 是中间 C。C# 将是 note
61,D note
62 等等。
let easymidi = require("easymidi")
let output = new easymidi.Output("mindiPort", true)
let sendNote = (noteValue, duration) => {
output.send("noteon", noteValue)
setTimeout(()=> {
output.send("noteoff", noteValue)
}, duration);
}
setInterval(() => {
let noteValue = {
note: 60, // **need to change this number depending on JSON.TEMPERATURE**
velocity: 100,
channel: 1
}
sendNote(noteValue, 500)
}, 2000);
我想要实现的是 if
/else
,其中演奏的音符取决于 JSON.TEMPERATURE
。
类似于:
if JSON.TEMPERATURE ==
8,打note
60
if JSON.TEMPERATURE ==
9、播放note
61
else
,播放音符 55
我还想 运行 每 5 秒 API 请求一次。
似乎您只需要一个 if
语句,例如
if(myJSON.TEMPERATURE == 8){
note = 60;
}
else if(myJSON.TEMPERATURE == 9){
note = 61;
}
else{
note = 55;
}
所以只需用这个代替您的 console.log
部分。
const fetch = require("node-fetch");
const api_url = 'https://www.placeholder_URL.com/api/getTemperature'
fetch(api_url)
.then((response) => {
return response.json();
})
.then((myJSON) => {
let note;
if(myJSON.TEMPERATURE == 8){
note = 60;
}
else if(myJSON.TEMPERATURE == 9){
note = 61;
}
else{
note = 55;
}
let noteValue = {
note: note,
velocity: 100,
channel: 1
}
sendNote(noteValue, 500)
});
let sendNote = (noteValue, duration) => {
output.send("noteon", noteValue)
setTimeout(()=> {
output.send("noteoff", noteValue)
}, duration);
}
您需要在 fetch 请求的 .then 内部调用 setInterval,这样便笺将在 api 请求完成并且您收到 JSON 响应后发送。
你目前是如何实现它的,它将每 2 秒调用一次 sendNote。此外,您希望每 5 秒执行一次 api 调用。这意味着我们要清除之前 api 调用的 sendNote 间隔,这就是间隔变量存在的原因,如果设置了间隔,我们将调用 clearInterval。
当然你可以通过将一些代码放入函数中来抽象它,使它看起来更漂亮,但这是它的要点
let interval;
const fetch = require("node-fetch");
const api_url = 'https://www.placeholder_URL.com/api/getTemperature'
setInterval(() =>
fetch(api_url)
.then((response) => {
return response.json();
})
.then((myJSON) => {
if (interval) {
clearInterval(interval);
}
interval = setInterval(() => {
let note;
if (myJSON.TEMPERATURE == 8) {
note = 60;
} else if (myJSON.TEMPERATURE == 9) {
note = 61;
} else {
note = 55;
}
let noteValue = {
note: note,
velocity: 100,
channel: 1
}
sendNote(noteValue, 500)
}, 2000);
}), 5000);
我有两个工作代码片段,我想 link 具有 if
/else
逻辑。代码的第一部分从网络 API 接收 JSON 响应。 JSON.TEMPERATURE
是所需的关键变量。
const fetch = require("node-fetch");
const api_url = 'https://www.placeholder_URL.com/api/getTemperature'
fetch(api_url)
.then((response) => {
return response.json();
})
.then((myJSON) => {
console.log(myJSON.TEMPERATURE); // **This returns a number between 8 and 15**
});
代码的第二部分播放 MIDI 音符。在下面的例子中,note
# 60 是中间 C。C# 将是 note
61,D note
62 等等。
let easymidi = require("easymidi")
let output = new easymidi.Output("mindiPort", true)
let sendNote = (noteValue, duration) => {
output.send("noteon", noteValue)
setTimeout(()=> {
output.send("noteoff", noteValue)
}, duration);
}
setInterval(() => {
let noteValue = {
note: 60, // **need to change this number depending on JSON.TEMPERATURE**
velocity: 100,
channel: 1
}
sendNote(noteValue, 500)
}, 2000);
我想要实现的是 if
/else
,其中演奏的音符取决于 JSON.TEMPERATURE
。
类似于:
if JSON.TEMPERATURE ==
8,打note
60
if JSON.TEMPERATURE ==
9、播放note
61
else
,播放音符 55
我还想 运行 每 5 秒 API 请求一次。
似乎您只需要一个 if
语句,例如
if(myJSON.TEMPERATURE == 8){
note = 60;
}
else if(myJSON.TEMPERATURE == 9){
note = 61;
}
else{
note = 55;
}
所以只需用这个代替您的 console.log
部分。
const fetch = require("node-fetch");
const api_url = 'https://www.placeholder_URL.com/api/getTemperature'
fetch(api_url)
.then((response) => {
return response.json();
})
.then((myJSON) => {
let note;
if(myJSON.TEMPERATURE == 8){
note = 60;
}
else if(myJSON.TEMPERATURE == 9){
note = 61;
}
else{
note = 55;
}
let noteValue = {
note: note,
velocity: 100,
channel: 1
}
sendNote(noteValue, 500)
});
let sendNote = (noteValue, duration) => {
output.send("noteon", noteValue)
setTimeout(()=> {
output.send("noteoff", noteValue)
}, duration);
}
您需要在 fetch 请求的 .then 内部调用 setInterval,这样便笺将在 api 请求完成并且您收到 JSON 响应后发送。
你目前是如何实现它的,它将每 2 秒调用一次 sendNote。此外,您希望每 5 秒执行一次 api 调用。这意味着我们要清除之前 api 调用的 sendNote 间隔,这就是间隔变量存在的原因,如果设置了间隔,我们将调用 clearInterval。
当然你可以通过将一些代码放入函数中来抽象它,使它看起来更漂亮,但这是它的要点
let interval;
const fetch = require("node-fetch");
const api_url = 'https://www.placeholder_URL.com/api/getTemperature'
setInterval(() =>
fetch(api_url)
.then((response) => {
return response.json();
})
.then((myJSON) => {
if (interval) {
clearInterval(interval);
}
interval = setInterval(() => {
let note;
if (myJSON.TEMPERATURE == 8) {
note = 60;
} else if (myJSON.TEMPERATURE == 9) {
note = 61;
} else {
note = 55;
}
let noteValue = {
note: note,
velocity: 100,
channel: 1
}
sendNote(noteValue, 500)
}, 2000);
}), 5000);