JavaScript日程显示问题求助:Uncaught ReferenceError: text未定义
Hey there! Let's work through the two main problems you're facing with your schedule display: the ReferenceError and the January date mismatch, plus a bonus tip to make your time comparison more reliable.
1. Fixing "Uncaught ReferenceError: text is not defined"
The error pops up because your text, text2, text3, etc. variables are only defined inside specific case blocks and time conditions. If the current date isn't in your listed schedule days, or the time falls outside the period you checked, these variables never get created—but your code still tries to use them later.
Quick fix: Initialize all these variables at the top of your function with default values, so they exist no matter what path the code takes:
function cambiar_imagen (){ // Initialize all variables with default values first var text = "No Schedule Available"; var text2 = ""; var text3 = ""; var text4 = ""; var text5 = ""; var pr = []; var hoy = new Date(); var hora = hoy.toLocaleTimeString(); var day = hoy.getDate(); var month = hoy.getMonth()+1; var year = hoy.getFullYear(); var dia = (day + "/" + month + "/" + year); // ... rest of your code }
Alternatively, add a default case to your switch statement to handle dates that don't match any of your listed cases:
switch(dia) { // ... all your existing case blocks default: text = "No Schedule Today"; text2 = ""; // set default values for other variables too break; }
2. Fixing January Date Matching Issues
Your January cases use dates like "8/1/2017"—but if the current year isn't 2017, your dia string will be something like "8/1/2024" which won't match. That's likely why your schedule stops working in January!
Even if the year is correct, comparing date strings is fragile (different locales or browser settings can change how dates are formatted). A better approach is to compare the numeric day, month, and year directly instead of concatenating them into a string:
function cambiar_imagen (){ // Initialize variables first as above... var hoy = new Date(); var currentDay = hoy.getDate(); var currentMonth = hoy.getMonth()+1; var currentYear = hoy.getFullYear(); // Create a set of valid schedule dates (objects with day/month/year) const validScheduleDates = [ // October 2017 {day:18, month:10, year:2017}, {day:20, month:10, year:2017}, {day:24, month:10, year:2017}, {day:26, month:10, year:2017}, // November 2017 {day:6, month:11, year:2017}, {day:8, month:11, year:2017}, // ... add all your other dates here // January 2017 {day:8, month:1, year:2017}, {day:10, month:1, year:2017}, {day:12, month:1, year:2017}, // ... rest of your dates ]; // Check if today is a valid schedule day const isScheduleDay = validScheduleDates.some(date => date.day === currentDay && date.month === currentMonth && date.year === currentYear ); if (isScheduleDay) { // Your schedule logic here (define pr array, check time periods) pr = [ ['Period 1 (08:30 - 09:45): Ms. Liz Edwards - Social Studies Psychology 11'], ['Period 2 (10:00 - 11:15): Ms. Liz Edwards - English 9A'], ['Period 3 (12:45 - 14:00): Mr. Joe Brogan - Social Studies 11 World History'], ['Period 4 (14:15 - 15:30): Ms. Liz Edwards - English 11 Language & Literature'], ['Enrichment (15:30 - 16:00): Mr. Toni Bauça - Homework Club 7+8'], ['Homeroom PM (16:45 - 17:30): David Pringuer - Homeroom 10 (T)'] ]; // Bonus: Fix time comparison (string comparison is unreliable!) const currentHours = hoy.getHours(); const currentMinutes = hoy.getMinutes(); const totalCurrentMinutes = currentHours * 60 + currentMinutes; const period1Start = 8*60 + 30; // 8:30 in total minutes const period1End = 9*60 + 45; // 9:45 in total minutes if (totalCurrentMinutes > period1Start && totalCurrentMinutes <= period1End) { text = "First Period - A Day"; text2 = "08:30 - 09:45"; text3 = "Social Studies Psychology 11"; text4 = "Ms. Liz Edwards"; text5 = "Next Periods<br><br>"; pr = pr[1][0]+"<br><br>"+pr[2][0]+"<br><br>"+pr[3][0]; } // Add other time period checks here... } // ... rest of your function (use the text variables to update the UI) }
Bonus: More Reliable Time Comparison
Using toLocaleTimeString() and comparing strings can fail if the browser uses a different time format (e.g., 24-hour time instead of 12-hour, or leading zeros like "08:30:00 AM"). Converting the time to total minutes since midnight makes the comparison consistent and error-proof, as shown in the code above.
内容的提问来源于stack exchange,提问作者Soru8

