49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
let data = [];
|
|
let currentIndex = -1;
|
|
|
|
// Load dữ liệu từ data.json
|
|
async function loadData() {
|
|
try {
|
|
const res = await fetch("data.json");
|
|
data = await res.json();
|
|
console.log("Load data OK:", data);
|
|
} catch (error) {
|
|
console.error("Lỗi load data:", error);
|
|
}
|
|
}
|
|
|
|
// Lấy câu hỏi (tiến / lùi)
|
|
function getRandomQuestion(direction = 0) {
|
|
if (data.length === 0) {
|
|
console.error("Chưa load data!");
|
|
return null;
|
|
}
|
|
|
|
// direction: 0 = tiến, 1 = lùi
|
|
if (direction === 0) currentIndex++;
|
|
else if (direction === 1) currentIndex--;
|
|
|
|
// Quay vòng
|
|
if (currentIndex >= data.length) currentIndex = 0;
|
|
if (currentIndex < 0) currentIndex = data.length - 1;
|
|
|
|
const correct = {
|
|
...data[currentIndex],
|
|
image: getImage(data[currentIndex].image)
|
|
};
|
|
|
|
window.items = { correct };
|
|
return window.items;
|
|
}
|
|
function voiceItem(name){
|
|
const newAudio = new Audio();
|
|
newAudio.src = "https://audio.senaai.vn/audio/en_female_1_"+name.toLowerCase()+".mp3";
|
|
newAudio.play();
|
|
}
|
|
|
|
|
|
// Hàm tạo URL ảnh
|
|
function getImage(name) {
|
|
return "https://senaai.vn/TDV_GAME/Khoi/items/" + name;
|
|
}
|