diff --git a/backend/templates/pmr.html b/backend/templates/pmr.html index d6fbb1f..cce6b7f 100644 --- a/backend/templates/pmr.html +++ b/backend/templates/pmr.html @@ -2687,11 +2687,13 @@ return; } - // 2. 중복 인쇄 방지 + // 2. 중복 인쇄 방지 (요청 전에 먼저 체크 & 추가!) if (window.printedSerials.has(preSerial)) { console.log('[AutoPrint] 이미 인쇄됨, 스킵:', preSerial); return; } + // 즉시 Set에 추가 (비동기 race condition 방지) + window.printedSerials.add(preSerial); // 3. 인쇄 진행 try { @@ -2710,20 +2712,22 @@ var data = await response.json(); if (data.success) { - // 인쇄 성공 → Set에 추가 (중복 방지) - window.printedSerials.add(preSerial); console.log('[AutoPrint] ' + now + ' ✅ 인쇄 완료:', preSerial, patientName); window.showToast('🖨️ ' + patientName, 'success'); - - // 5분 후 Set에서 제거 (메모리 관리) - setTimeout(function() { - window.printedSerials.delete(preSerial); - }, 5 * 60 * 1000); } else { + // 실패 시 Set에서 제거 (재시도 가능하도록) + window.printedSerials.delete(preSerial); console.error('[AutoPrint] ' + now + ' ❌ 실패:', preSerial, data.error); window.showToast('인쇄 실패: ' + patientName, 'error'); } + + // 5분 후 Set에서 제거 (메모리 관리) + setTimeout(function() { + window.printedSerials.delete(preSerial); + }, 5 * 60 * 1000); } catch (err) { + // 오류 시 Set에서 제거 + window.printedSerials.delete(preSerial); console.error('[AutoPrint] 오류:', preSerial, err); window.showToast('인쇄 오류', 'error'); }