The problem you described is this:
- When scheduling videos starting today, you correctly skip already passed times on today.
- But when starting scheduling from a future day (like 7 days later), the code skips the initial few scheduled times, effectively "dropping" some videos that should have been scheduled at those times on that start day.
- Also, when skip_days > 0, days get skipped properly but the scheduling start could be off (e.g., it might not schedule exactly on the intended start date and time slots).
Why this happens?
Your scheduling logic reuses the same scheduleQueue array for all days, but filtering only happens for the very first day (today). When you set a start date beyond today, you always assign scheduleQueue = [...selectedTimes]; without adjusting for any "passed times" relative to that start date, so it always removes the first few time slots incorrectly inside the loop, or resets scheduleQueue after skipping days, losing the exact alignment.
How to fix it?
You want to:
- When starting from a future date, schedule all times for that day (no times skipped).
- Whenever
scheduleQueue is exhausted, increment the base day offset by (1 + skip_days) and reset scheduleQueue to all scheduled times (full set).
- You should never skip times on the start date unless that day is today and times already passed before now.
- So essentially:
- On start date == today, filter to keep only future times.
- On start date != today, keep all times.
- Then, for subsequent scheduled days with skip_days, always assign full list again without filtering.
Updated code snippet:
Replace your scheduling loop with the following fix in your script for the form submit event:
document.getElementById('uploadForm').addEventListener('submit', async function(event) {
event.preventDefault();
const files = document.getElementById('video_files').files;
if (!files.length) {
alert("Please select video files.");
return;
}
const selectedTimes = JSON.parse(document.getElementById('dailyTimesInput').value || '[]');
if (!selectedTimes.length) {
alert("Please select at least one upload time.");
return;
}
const skipDays = parseInt(document.getElementById('skip_days').value) || 0;
const startDateInput = document.getElementById('start_schedule_date').value;
if (!startDateInput) {
alert("Please select a start scheduling date.");
return;
}
const startDate = new Date(startDateInput + "T00:00:00");
const now = new Date();
const form = event.target;
const progressContainer = document.getElementById('progressContainer');
const overallProgress = document.getElementById('overallProgress');
const progressText = document.getElementById('progressText');
const fileStatusContainer = document.getElementById('fileStatusContainer');
progressContainer.style.display = 'block';
fileStatusContainer.innerHTML = '';
overallProgress.style.width = '0%';
progressText.textContent = '0%';
// Prepare initial schedule queue
let scheduleQueue;
// If start date is today, filter out passed times
if (startDate.toDateString() === now.toDateString()) {
scheduleQueue = selectedTimes.filter(t => {
const [h, m] = t.split(':');
const temp = new Date();
temp.setHours(parseInt(h), parseInt(m), 0, 0);
return temp > now;
});
} else {
// For any date in the future, keep all times untouched
scheduleQueue = [...selectedTimes];
}
if (scheduleQueue.length === 0) {
alert("No upload times remain on the start scheduling day after filtering out past times.");
return;
}
let baseDayOffset = 0; // days added to startDate
let uploadedCount = 0;
let successfulUploads = 0;
let failedUploads = 0;
for (let i = 0; i < files.length; i++) {
if (scheduleQueue.length === 0) {
// Move forward skipDays + 1 days
baseDayOffset += (1 + skipDays);
// Reset scheduleQueue with full times on new day -- no filtering needed for future days
scheduleQueue = [...selectedTimes];
}
const scheduledTimeStr = scheduleQueue.shift();
const [hh, mm] = scheduledTimeStr.split(':');
const scheduledDate = new Date(startDate);
scheduledDate.setDate(startDate.getDate() + baseDayOffset);
scheduledDate.setHours(parseInt(hh), parseInt(mm), 0, 0);
// Format datetime string for server
const yyyy = scheduledDate.getFullYear();
const MMm = String(scheduledDate.getMonth() + 1).padStart(2, '0');
const dd = String(scheduledDate.getDate()).padStart(2, '0');
const HH = String(scheduledDate.getHours()).padStart(2, '0');
const mmStr = String(scheduledDate.getMinutes()).padStart(2, '0');
const localDatetime = `${yyyy}-${MMm}-${dd}T${HH}:${mmStr}`;
// Prepare form data and upload logic as before...
// (same as your existing code below)
const formData = new FormData();
formData.append('channel_id', form.channel_id.value);
const startIndex = parseInt(form.start_index.value) || 1;
const customTitle = `${form.title.value} (${startIndex + i})`;
formData.append('title', customTitle);
formData.append('description', form.description.value);
formData.append('tags', form.tags.value);
formData.append('scheduled_time', localDatetime);
formData.append('timezone_offset', new Date().getTimezoneOffset());
formData.append('video_files', files[i]);
const statusDiv = document.createElement('div');
statusDiv.className = 'file-status';
statusDiv.innerHTML = `<span class="status-icon">⏳</span><span class="file-name">${files[i].name}</span><span class="status-text" style="margin-left: auto;">Uploading...</span>`;
fileStatusContainer.appendChild(statusDiv);
try {
const response = await fetch('/upload', { method: 'POST', body: formData });
const data = await response.json();
if (data.success) {
statusDiv.querySelector('.status-icon').textContent = '✅';
statusDiv.querySelector('.status-text').textContent = 'Uploaded successfully';
successfulUploads++;
} else {
throw new Error(data.error || 'Upload failed');
}
} catch (error) {
statusDiv.querySelector('.status-icon').textContent = '❌';
statusDiv.querySelector('.status-text').textContent = `Error: ${error.message}`;
failedUploads++;
}
uploadedCount++;
const progress = Math.round((uploadedCount / files.length) * 100);
overallProgress.style.width = `${progress}%`;
progressText.textContent = `${progress}%`;
}
const completionMsg = document.createElement('div');
completionMsg.innerHTML = `<strong>${successfulUploads} of ${files.length} uploaded successfully.</strong>${failedUploads > 0 ? ` (${failedUploads} failed)` : ''}`;
fileStatusContainer.appendChild(completionMsg);
});
Summary of key changes:
- On the initial day (startDate), filter out passed times only if that day is today.
- When scheduling advances by
skipDays + 1, always reset scheduleQueue to all possible times ([...]selectedTimes), without any filtering for passed times (all are future days).
- Added a safety
if(scheduleQueue.length === 0) check after initial filtering to stop if there's no slot to schedule (e.g., all times passed on today, but user still tries to schedule).
This approach preserves all intended scheduled time slots starting from the explicitly chosen start day, properly applies skip days in between, and does not unintentionally skip the wrong time slots on a future start scheduling date.
If you want, I can help you fully integrate this or test with more cases. Let me know!