It looks like there is a stray closing curly brace } at the end that doesn't match any opening brace, causing a syntax error. Also, since you want the page to reload and you are setting localStorage beforehand, your code is mostly correct.
Here’s the fixed version of your code without syntax errors, and ensuring the reload happens when 'e' is pressed:
The "unexpected end of input" syntax error usually means that somewhere your code has a missing brace, parenthesis, or quote — the parser hit the end of the file but was expecting more.
User
Assistant
Make sure the variable newLevel is defined and accessible in this scope, otherwise it will cause a ReferenceError.
If you want the event listener to respond every time the 'e' key is pressed (and not just once), remove the { once: true } option:
javascript
document.addEventListener('keydown', function(event) {
if (event.key.toLowerCase() === 'e') {
displayMessage("Refreshing to Level " + newLevel + "...");
localStorage.setItem('currentLevel', newLevel);
console.log("Page refreshed for Level " + newLevel + ".");
location.reload();
}
});
Summary of fixes:
Removed the extra closing brace.
Ensure newLevel is defined.
Decide if you want { once: true } to limit the listener or not.
The snippet you shared is actually fine as-is (assuming everything outside it is balanced). Here is a complete example that should not throw that syntax error:
js
// Make sure newLevel is defined globally or before this script runs
let newLevel = 2;
function displayMessage(message) {
console.log(message); // Replace this with your actual display logic
}
document.addEventListener('keydown', function(event) {
if (event.key.toLowerCase() === 'e') {
displayMessage("Refreshing to Level " + newLevel + "...");
localStorage.setItem('currentLevel', newLevel);
console.log("Page refreshed for Level " + newLevel + ".");
location.reload();
}
}, { once: true });
If you still get "unexpected end of input," consider:
Check if earlier parts of your script are missing closing }, ), or ].
Are you wrapping this in another function or block that is unclosed?
Are you accidentally cutting off a multi-line string or template literal?
Minimal Reproducible Example
Copy-paste this exact code into a new HTML file or JS console:
Press e in this page and it should show an alert, then reload the page. It will not give a syntax error.
If your error persists
Paste your entire relevant code or file causing the error, so I can help you spot where the syntax mistake is.
Summary:
Remove extra } or missing braces in your actual file.
Make sure you have properly closed all blocks before this snippet.
Define variables like newLevel before usage.
Test with the provided minimal code to isolate problem.
Let me know if you want me to check your whole code!
also fix this it keeps spamming next tphase immediately with n o cool down keeping the boss shield on and off after phase is down it turns off letting player attacks and before next phase starts after 5 seocudns it turns shield back on please fix this
async function cycleBossAttacks() {
if (bossHealth <= 0) {
if (!bossDefeatedMessageDisplayed) {
bossDefeatedMessageDisplayed = true;
displayMessage("Boss Defeated!", 5);
}
return;
}
// Initialize on fresh cycle
if (!phaseStartTime) {
phaseStartTime = Date.now();
bossAttackPhase = 0;
}
// Shield ON at start of phase
bossAttacking = true;
bossShielded = true;
boss.classList.add('boss-shielded');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Wrap both promise and sync attack functions to await uniformly
const wrapAttack = (attackFunc) => {
return () => new Promise(resolve => {
const result = attackFunc();
if (result && typeof result.then === 'function') result.then(resolve);
else resolve();
});
};
// Run an attack with await
async function runAttack(attackFunc) {
await wrapAttack(attackFunc)();
}
// Turn shield OFF, display message, wait, then turn shield ON again
async function postPhaseShieldOffWait(delayMs = 5000) {
bossShielded = false;
boss.classList.remove('boss-shielded');
displayMessage("Boss shield is OFF! Attack now!", 5);
await sleep(delayMs);
bossShielded = true;
boss.classList.add('boss-shielded');
}
// Phase 4 and 5 use preAttackWithMessage from your original code:
async function preAttackWithMessage(phaseNumber, phaseAttackFunction, messages = []) {
const key = L${level}_P${phaseNumber}_A${bossAttackPhase};
if (!shownPreMessages[key]) {
for (const msg of messages) {
displayMessage(msg.text, msg.durationSeconds);
await sleep(msg.durationSeconds * 1000);
}
shownPreMessages[key] = true;
}
await phaseAttackFunction();
displayMessage(Shield active for 5 seconds after phase ${phaseNumber} attack, 5);
await sleep(5000);
}
// Execute attack based on level and phase
switch(level) {
case 1:
switch(bossAttackPhase) {
case 0: await runAttack(bossEnemyBarrage); break;
case 1: await runAttack(bossTargetSlam); break;
case 2: await runAttack(bossNewPower); break;
case 3: await runAttack(bossExplosionRain); break;
}
break;
code
case 2:
switch(bossAttackPhase) {
case 0: await runAttack(bossPhaseTwoBarrage); break;
case 1: await runAttack(bossPhaseTwoTargetSlam); break;
case 2: await runAttack(bossPhaseTwoNewPower); break;
case 3: await runAttack(bossPhaseTwoExplosionRain); break;
}
break;
case 3:
if (bossAttackPhase === 0 && (Date.now() - phaseStartTime) < 13000) {
startPhaseThree();
return;
}
switch(bossAttackPhase) {
case 0: await runAttack(bossPhaseThree); break;
case 1: await runAttack(bossPhaseTwoExplosionRain2); break;
case 2: await runAttack(bossPhaseTwoTargetSlam3); break;
case 3: await runAttack(bossPhaseTwoNewPower2); break;
case 4: await runAttack(bossPhaseTwoTargetSlam2); break;
case 5: await runAttack(bossPhase5); break;
}
break;
case 4:
if (bossAttackPhase === 0 && (Date.now() - phaseStartTime) < 13000) {
startPhaseFour();
return;
}
switch (bossAttackPhase) {
case 0:
await preAttackWithMessage(4, wrapAttack(EnemyBarrage), [{
text: "Lets start with something simple!",
durationSeconds: 4
}]);
break;
case 1:
await preAttackWithMessage(4, wrapAttack(laserPhaseMove), [{
text: "Incredible, a nice worthy foe.",
durationSeconds: 3
}]);
break;
case 2:
await preAttackWithMessage(5, wrapAttack(bossPhase5), [{
text: "Well nobody has got this far before, your doing somehow great...",
durationSeconds: 4
}]);
break;
case 3:
await preAttackWithMessage(4, wrapAttack(bossPhaseTwoExplosionRain2), [{
text: "How are you NOT DEAD YET?",
durationSeconds: 3
}]);
break;
case 4:
await preAttackWithMessage(4, wrapAttack(bossPhaseThree), [{
text: "Your actully getting annoying now...",
durationSeconds: 3
}]);
break;
case 5:
await preAttackWithMessage(4, wrapAttack(slidingLaserPhase), [{
text: "Alright let me stop talking now its game on.",
durationSeconds: 4
}]);
break;
}
break;
case 5:
if (bossAttackPhase === 0 && (Date.now() - phaseStartTime) < 13000) {
cycleBossAttacks();
return;
}
switch (bossAttackPhase) {
case 0:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackA), [{
text: "Phase 5 begins! Watch closely!",
durationSeconds: 4
}]);
break;
case 1:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackB), [{
text: "Don't blink!",
durationSeconds: 3
}]);
break;
case 2:
await preAttackWithMessage(5, wrapAttack(bossPhase5), [{
text: "Well nobody has got this far before, your doing somehow great...",
durationSeconds: 3
}]);
break;
case 3:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackD));
break;
case 4:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackE));
break;
case 5:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackF));
break;
}
break;
}
// After attack finishes:
await postPhaseShieldOffWait();
// Advance to next phase
bossAttackPhase = (bossAttackPhase + 1) % maxBossMoves;
// Start next attack cycle recursively
cycleBossAttacks();
}
merge this to
async function cycleBossAttacks() {
if (bossHealth <= 0) {
if (!bossDefeatedMessageDisplayed) {
bossDefeatedMessageDisplayed = true;
displayMessage("Boss Defeated!", 5);
}
return;
}
code
// Initialize phaseStartTime and phase for fresh phases if not present
if (!phaseStartTime) {
phaseStartTime = Date.now();
bossAttackPhase = 0;
}
// Shield on at beginning of attack cycle
bossAttacking = true;
bossShielded = true;
boss.classList.add('boss-shielded');
// Number of boss moves for level
const maxBossMoves = (level === 1) ? 4 : (level === 2) ? 4 : 6;
// Define async helper to handle pre-attack messaging & shields for phases 4 & 5:
async function preAttackWithMessage(phaseNumber, phaseAttackFunction, messages = []) {
// Make a unique key for current attack in phase and level
const key = L${level}_P${phaseNumber}_A${bossAttackPhase};
code
// Only show messages if not shown before for this key
if (!shownPreMessages[key]) {
for (const msg of messages) {
displayMessage(msg.text, msg.durationSeconds);
await sleep(msg.durationSeconds * 1000);
}
shownPreMessages[key] = true; // Mark as shown
}
// Attack now (do it every time)
await phaseAttackFunction();
// After attack keep shield on for 5 seconds
displayMessage(`Shield active for 5 seconds after phase ${phaseNumber} attack`, 5);
await sleep(5000);
}
code
}
Got it. You want to merge the structured async/await version with clean cooldown management so the boss doesn't instantly spam next phase, correctly toggles shield OFF after phase, waits ~5 sec before shield ON again, then moves on only after. Currently, your recursive cycles and timers overlap, causing shield flicker and phase advancing without cooldown.
Core issues causing your problem
Your code calls cycleBossAttacks() recursively at the end without awaiting delays properly → so it immediately moves next phase/swaps shield too fast.
In your merged version, many boss attacks are not awaited (e.g., bossExplosionRain() without await). If those are async or delayed, your logic ignores that delay.
You often do setTimeout to turn off shield after 5sec without awaiting it, so the main function continues immediately.
You mix sync and async functions, causing shield and phase changes to run too fast.
How to fix:
You need an async function that:
Awaits the actual attack (which should be async or wrapped as promise)
Waits cooldowns with await sleep(...) (do NOT use setTimeout in async flow for delays)
Maintains shield ON during attack+cooldown
Turns shield OFF for cooldown window so player can attack
Turns shield back ON after cooldown before next phase
Advances phase only after cooldown
Calls itself recursively after all those await delays
Complete revised example combining your async approach with cooldown control
Below is a cleaned-up, fully async cycleBossAttacks function with proper awaiting of each attack phase and clean shield toggling & cooldowns:
js
// Utility sleep helper
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Wrap possibly sync attack functions to always return Promise for await:
const wrapAttack = (attackFunc) => async () => {
// If already returns promise:
const res = attackFunc();
if (res && typeof res.then === 'function') {
await res;
}
// else just continue immediately assuming sync
};
// Display messages (use your own display logic)
function displayMessage(text, seconds = 3) {
// Example: console log
console.log(text);
// If you have UI - show message for duration as needed
}
let bossAttackPhase = 0;
let phaseStartTime = null;
let bossShielded = false;
let bossAttacking = false;
const shownPreMessages = {}; // track pre-attack messages shown for uniqueness
let bossDefeatedMessageDisplayed = false;
// Dummy example functions for attacks - replace with your real ones
async function bossEnemyBarrage() { console.log('bossEnemyBarrage attack'); await sleep(1500); }
async function bossTargetSlam() { console.log('bossTargetSlam attack'); await sleep(1500); }
async function bossNewPower() { console.log('bossNewPower attack'); await sleep(1500); }
async function bossExplosionRain() { console.log('bossExplosionRain attack'); await sleep(1500); }
// ...
// Your boss element for adding/removing shield class
const boss = document.querySelector('.boss'); // Example, adjust accordingly
// Your level and bossHealth state (should be managed externally)
let level = 1; // example level; change as appropriate
let bossHealth = 100; // example boss health; change during gameplay
// The main async function controlling attack cycles:
async function cycleBossAttacks() {
if (bossHealth <= 0) {
if (!bossDefeatedMessageDisplayed) {
bossDefeatedMessageDisplayed = true;
displayMessage("Boss Defeated!", 5);
}
return; // stop cycling attacks!
}
// Initialize phase start time on fresh cycles
if (!phaseStartTime) {
phaseStartTime = Date.now();
bossAttackPhase = 0;
}
// Number of attacks per level
const maxBossMoves = (level === 1 || level === 2) ? 4 : 6;
// Function to toggle shield ON
function shieldOn() {
bossShielded = true;
bossAttacking = true;
if (boss) boss.classList.add('boss-shielded');
}
// Function to toggle shield OFF
function shieldOff() {
bossShielded = false;
bossAttacking = false;
if (boss) boss.classList.remove('boss-shielded');
}
// Function for post phase cooldown - shield off, wait 5sec, shield on before next phase
async function postPhaseCooldown() {
shieldOff();
displayMessage("Boss shield is OFF! Attack now!", 5);
await sleep(5000); // Player has 5 seconds to attack
shieldOn();
}
// Your preAttackWithMessage function for phases 4 & 5
async function preAttackWithMessage(phaseNumber, phaseAttackFunction, messages = []) {
const key = `L${level}_P${phaseNumber}_A${bossAttackPhase}`;
if (!shownPreMessages[key]) {
for (const msg of messages) {
displayMessage(msg.text, msg.durationSeconds);
await sleep(msg.durationSeconds * 1000);
}
shownPreMessages[key] = true;
}
await phaseAttackFunction();
displayMessage(`Shield active for 5 seconds after phase ${phaseNumber} attack`, 5);
await sleep(5000);
}
shieldOn();
// Select attack to run based on level and phase (await attack so it finishes)
switch (level) {
case 1:
switch (bossAttackPhase) {
case 0: await bossEnemyBarrage(); break;
case 1: await bossTargetSlam(); break;
case 2: await bossNewPower(); break;
case 3: await bossExplosionRain(); break;
}
break;
case 2:
switch (bossAttackPhase) {
case 0: await bossPhaseTwoBarrage(); break;
case 1: await bossPhaseTwoTargetSlam(); break;
case 2: await bossPhaseTwoNewPower(); break;
case 3: await bossPhaseTwoExplosionRain(); break;
}
break;
case 3:
if (bossAttackPhase === 0 && (Date.now() - phaseStartTime) < 13000) {
startPhaseThree();
return;
}
switch (bossAttackPhase) {
case 0: await bossPhaseThree(); break;
case 1: await bossPhaseTwoExplosionRain2(); break;
case 2: await bossPhaseTwoTargetSlam3(); break;
case 3: await bossPhaseTwoNewPower2(); break;
case 4: await bossPhaseTwoTargetSlam2(); break;
case 5: await bossPhase5(); break;
}
break;
case 4:
if (bossAttackPhase === 0 && (Date.now() - phaseStartTime) < 13000) {
startPhaseFour();
return;
}
switch (bossAttackPhase) {
case 0:
await preAttackWithMessage(4, wrapAttack(EnemyBarrage), [{ text: "Lets start with something simple!", durationSeconds: 4 }]);
break;
case 1:
await preAttackWithMessage(4, wrapAttack(laserPhaseMove), [{ text: "Incredible, a nice worthy foe.", durationSeconds: 3 }]);
break;
case 2:
await preAttackWithMessage(5, wrapAttack(bossPhase5), [{ text: "Well nobody has got this far before, your doing somehow great...", durationSeconds: 4 }]);
break;
case 3:
await preAttackWithMessage(4, wrapAttack(bossPhaseTwoExplosionRain2), [{ text: "How are you NOT DEAD YET?", durationSeconds: 3 }]);
break;
case 4:
await preAttackWithMessage(4, wrapAttack(bossPhaseThree), [{ text: "Your actully getting annoying now...", durationSeconds: 3 }]);
break;
case 5:
await preAttackWithMessage(4, wrapAttack(slidingLaserPhase), [{ text: "Alright let me stop talking now its game on.", durationSeconds: 4 }]);
break;
}
break;
case 5:
if (bossAttackPhase === 0 && (Date.now() - phaseStartTime) < 13000) {
cycleBossAttacks(); // or startPhaseFive(); depends on your logic
return;
}
switch (bossAttackPhase) {
case 0:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackA), [{ text: "Phase 5 begins! Watch closely!", durationSeconds: 4 }]);
break;
case 1:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackB), [{ text: "Don't blink!", durationSeconds: 3 }]);
break;
case 2:
await preAttackWithMessage(5, wrapAttack(bossPhase5), [{ text: "Well nobody has got this far before, your doing somehow great...", durationSeconds: 3 }]);
break;
case 3:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackD));
break;
case 4:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackE));
break;
case 5:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackF));
break;
}
break;
default:
console.warn('Unknown level', level);
}
// After each attack (except phases 4 & 5 since preAttackWithMessage has cooldown) do cooldown:
// For levels 1, 2, 3, do cooldown here:
if (level < 4) {
await postPhaseCooldown();
}
// Advance phase for next attack
bossAttackPhase = (bossAttackPhase + 1) % maxBossMoves;
// Call function recursively to continue cycles after cooldown & shield toggle
cycleBossAttacks();
}
Important notes:
All your boss attack functions must be async or wrapped to await properly otherwise cycle will move on immediately.
You must NOT use setTimeout in async flow for logic delays but rather await sleep(...).
Shield toggling is tightly coupled with cooldown: shield ON during attack+shield ON cooldown, then shield OFF for player attack window, then shield ON again on next cycle.
Recursion (cycleBossAttacks()) only happens after cooldown finishes.
For phase 4 & 5 with preAttackWithMessage, cooldown and shield logic is inside the async preAttackWithMessage, so no need to do cooldown in main cycle for those levels.
Your startPhaseThree() or similar functions should handle their logic in a way that doesn’t interfere with this cycle, or you can integrate them better.
If you want, I can help you refactor your boss attack functions to ensure they return promises so your cycle waits for them properly.
TL;DR
Replace your complex mixture of setTimeout and callbacks with this fully async/await flow:
Await your boss attack functions
Await cooldown phase with shield off & on delays
Advance phase only after cooldown ends
Recursive call after all awaits so next phase awaits full cooldown
If you want me to fix your exact original attack functions too for proper async support, just ask!
// Because phases 4 and 5 need to wait and show messages with shield ON before attack,
// make sure your phase functions return Promise for awaiting or wrap if needed.
// If they don't, I'll wrap them here:
// Wrappers for your attack functions to make them async
const wrapAttack = (attackFunc) => {
return () => new Promise(resolve => {
attackFunc();
resolve();
});
};
switch (level) {
case 1:
switch (bossAttackPhase) {
case 0:
bossEnemyBarrage();
break;
case 1:
bossTargetSlam();
break;
case 2:
bossNewPower();
break;
case 3:
bossExplosionRain();
break;
}
// Shield off after 5 seconds for these phases too
setTimeout(() => {
bossShielded = false;
boss.classList.remove('boss-shielded');
displayMessage("Boss shield is OFF! Attack now!");
}, 5000);
bossAttackPhase = (bossAttackPhase + 1) % maxBossMoves;
break;
case 2:
switch (bossAttackPhase) {
case 0:
bossPhaseTwoBarrage();
break;
case 1:
bossPhaseTwoTargetSlam();
break;
case 2:
bossPhaseTwoNewPower();
break;
case 3:
bossPhaseTwoExplosionRain();
break;
}
setTimeout(() => {
bossShielded = false;
boss.classList.remove('boss-shielded');
displayMessage("Boss shield is OFF! Attack now!");
}, 5000);
bossAttackPhase = (bossAttackPhase + 1) % maxBossMoves;
break;
case 3:
if (bossAttackPhase === 0 && (Date.now() - phaseStartTime) < 13000) {
startPhaseThree();
return;
}
switch (bossAttackPhase) {
case 0:
bossPhaseThree();
break;
case 1:
bossPhaseTwoExplosionRain2();
break;
case 2:
bossPhaseTwoTargetSlam3();
break;
case 3:
bossPhaseTwoNewPower2();
break;
case 4:
bossPhaseTwoTargetSlam2();
break;
case 5:
bossPhase5();
break;
}
setTimeout(() => {
bossShielded = false;
boss.classList.remove('boss-shielded');
displayMessage("Boss shield is OFF! Attack now!");
}, 5000);
bossAttackPhase = (bossAttackPhase + 1) % maxBossMoves;
break;
case 4:
// Example startup condition before attacks on phase 0
if (bossAttackPhase === 0 && (Date.now() - phaseStartTime) < 13000) {
startPhaseFour();
return;
}
// Define messages before each phase 4 attack optionally:
switch (bossAttackPhase) {
case 0:
// Example: Pre-attack messages before attack
await preAttackWithMessage(4, wrapAttack(EnemyBarrage), [{
text: "Lets start with something simple!",
durationSeconds: 4
}]);
break;
case 1:
await preAttackWithMessage(4, wrapAttack(laserPhaseMove), [{
text: "Incredible, a nice worthy foe.",
durationSeconds: 3
}]);
break;
case 2:
await preAttackWithMessage(5, wrapAttack(bossPhase5), [{
text: "Well nobody has got this far before, your doing somehow great...",
durationSeconds: 4
}]);
break;
case 3:
await preAttackWithMessage(4, wrapAttack(bossPhaseTwoExplosionRain2), [{
text: "How are you NOT DEAD YET?",
durationSeconds: 3
}]);
break;
case 4:
await preAttackWithMessage(4, wrapAttack(bossPhaseThree), [{
text: "Your actully getting annoying now...",
durationSeconds: 3
}]);
break;
case 5:
await preAttackWithMessage(4, wrapAttack(slidingLaserPhase), [{
text: "Alright let me stop talking now its game on.",
durationSeconds: 4
}]);
break;
}
bossAttackPhase = (bossAttackPhase + 1) % maxBossMoves;
break;
case 5:
// Example start condition if needed
if (bossAttackPhase === 0 && (Date.now() - phaseStartTime) < 13000) {
cycleBossAttacks(); // Assuming you have this
return;
}
// Define messages + attacks for phase 5 (you can customize this)
switch (bossAttackPhase) {
case 0:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackA), [{
text: "Phase 5 begins! Watch closely!",
durationSeconds: 4
}]);
break;
case 1:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackB), [{
text: "Don't blink!",
durationSeconds: 3
}]);
break;
case 2:
await preAttackWithMessage(5, wrapAttack(bossPhase5)), [{
text: "Well nobody has got this far before, your doing somehow great...",
durationSeconds: 3
}]);
break;
case 3:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackD));
break;
case 4:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackE));
break;
case 5:
await preAttackWithMessage(5, wrapAttack(bossPhaseFiveAttackF));
break;
}
bossAttackPhase = (bossAttackPhase + 1) % maxBossMoves;
break;
}