Estoy desarrollando una extensión de Chrome y tengo un botón. Cuando se hace clic en este botón, obtiene una URL aleatoria de JSON. No quiero que la última URL sea la misma que la URL aleatoria recibida actualmente. ¿Cómo puedo obtener una URL aleatoria hasta que los datos sean diferentes?
JavaScript
var music; var lastMusic; // Suppose I got this data. window.addEventListener("load",function() { fetch('***data.json', { method: 'GET' }) .then(function(response) { return response.json(); }) .then(function(json) { try { if(json.last_version !== chrome.runtime.getManifest().version) { $id("new-update").style.display = "block"; } music = json.musics[Math.floor(Math.random()*(json.musics.length + 1))].url; } catch (error) {} }); },false);ACTUALIZAR
Resolví el problema, pero Chrome arroja un error Uncaught TypeError: Cannot read properties of undefined (reading 'url')
var music; var lastMusic; window.addEventListener("load",function() { fetch('https://kortopal.github.io/data/11a/data.json', { method: 'GET' }) .then(function(response) { return response.json(); }) .then(function(json) { try { var musicInterval; if(json.last_version !== chrome.runtime.getManifest().version) { $id("new-update").style.display = "block"; } musicInterval = setInterval(function() { music = json.musics[Math.floor(Math.random()*(json.musics.length + 1))].url; // Error Line if(music !== lastMusic){ clearInterval(musicInterval); lastMusic = music; setStorage(); loadStorage(); } }, 100); } catch (error) {} }); },false);