我如何只获得一个唯一值(音符)来播放 - 不重复 Javascript

How do I get only a unique value (note) to play - no repeats Javascript

我有一个完美的脚本。基本上,我每 5 秒轮询一个 API myJson.Temperature,并根据返回值播放一个 MIDI 音符。值介于 0 和 16 之间。这是脚本:

let easymidi = require("easymidi")
let output = new easymidi.Output("mindiPort", true)
let interval;
const fetch = require("node-fetch");

let sendNote = (noteValue, duration) => {
    output.send("noteon", noteValue)

    setTimeout(()=> {
        output.send("noteoff", noteValue)
    }, duration);
}

const api_url = 'https://www.placeholder_url.com?timeout=1000'
setInterval(() =>
fetch(api_url)
   .then((response) => {
      return response.json();
   })

.then((myJson) => {
      let note;
      if (myJson.temperature == 0) {
        note = 24;
      } else if (myJson.temperature == 2) {
        note = 25;
      } else if (myJson.temperature == 4) {
        note = 26;
      } else if (myJson.temperature == 6) {
        note = 27;
      } else if (myJson.temperature == 8) {
        note = 28;
      } else if (myJson.temperature == 10) {
        note = 29;
      } else if (myJson.temparature == 12) {
        note = 30;
      } else if (myJson.temperature == 14) {
        note = 31;
      } else {
        note = 32;
      }
      let noteValue = {
         note: note,
         velocity: 100,
         channel: 1
       }
       sendNote(noteValue, 500)
   
 }), 5000); // API call every 5 seconds **This will also play a (same) note every five seconds
//  if conditions are met**

我怎样才能让音符只播放一次,即; - 如果后续轮询完成并且 returns 相同的值,那么在返回唯一值之前可以忽略该注释吗?我看过 filteronlyUnique 但我不确定如何将它们合并到代码中。提前谢谢你。

我会采用一种简单的方法并跟踪最后发出的音符:

编辑:我的代码是错误的,因为它正在检查复杂对象的相等性,修复了它

let easymidi = require("easymidi")
let output = new easymidi.Output("mindiPort", true)
let interval;
const fetch = require("node-fetch");

let lastNote = null;
let sendNote = (noteValue, duration) => {
    output.send("noteon", noteValue)

    setTimeout(()=> {
        output.send("noteoff", noteValue)
    }, duration);
}

const api_url = 'https://www.placeholder_url.com?timeout=1000'
setInterval(() =>
fetch(api_url)
   .then((response) => {
      return response.json();
   })

.then((myJson) => {
      let note;
      if (myJson.temperature == 0) {
        note = 24;
      } else if (myJson.temperature == 2) {
        note = 25;
      } else if (myJson.temperature == 4) {
        note = 26;
      } else if (myJson.temperature == 6) {
        note = 27;
      } else if (myJson.temperature == 8) {
        note = 28;
      } else if (myJson.temperature == 10) {
        note = 29;
      } else if (myJson.temparature == 12) {
        note = 30;
      } else if (myJson.temperature == 14) {
        note = 31;
      } else {
        note = 32;
      }
      if (lastNote !== note) {
          let noteValue = {
             note: note,
             velocity: 100,
             channel: 1
           }
           sendNote(noteValue, 500)
      }
      lastNote = note;
   
 }), 5000); // API call every 5 seconds **This will also play a (same) note every five seconds
//  if conditions are met**