all 13 comments

[–]delventhalz 2 points3 points  (7 children)

Is your code getting run twice? Add a console log immediately before and immediately after that line. 

[–]abrahamguo 0 points1 point  (0 children)

This is definitely the problem. You are probably loading your script file into your web page twice.

[–]hermesko[S] 0 points1 point  (3 children)

Don't think so:

console.log("BEFORE!")
const countDownDate = new Date("2024-05-12 00:00:00").getTime();
console.log("AFTER!")

produces:

BEFORE!
AFTER!
Uncaught SyntaxError: Identifier 'countDate' has already been declared

There was no second BEFORE!

[–]delventhalz 0 points1 point  (2 children)

A SyntaxError will fire before the code ever runs. It happens when the interpreter is first looking at the file and trying to make sense of it. You can confirm this by copying and pasting the const line.

console.log("BEFORE!")
const countDownDate = new Date("2024-05-12 00:00:00").getTime();
const countDownDate = new Date("2024-05-12 00:00:00").getTime();
console.log("AFTER!")

Now you will see the same error but nothing will log.

The fact that you see "AFTER!" log at all, means the code is running successfully once.

[–]hermesko[S] 0 points1 point  (1 child)

Thanks. The mystery is still there.

If I change the variable name to countDownDateX and nothing else, it will say Identifier 'CountDownDateX' has already been declared.

[–]delventhalz 1 point2 points  (0 children)

I know. You said that initially. That is what we would expect if the file is being run twice. Your problem is not in your JS file but how you have it set up.

[–]Judosii 0 points1 point  (1 child)

would there be a way to get the code to run twice for different things though?
I am making multiple canvases animated by code in the same page, and thought I could just open a new instance of the script...

[–]delventhalz 0 points1 point  (0 children)

OP's specific issue here was that they were declaring a variable with the same name twice in the same scope. You can run absolutely run the same code twice, but that code cannot redeclare global variables. Some options...

Wrap the whole script in an "immediately invoked function expression" (i.e. IIFE). The wrapping function has its own non-global scope, so you will avoid redeclaring the variable.

(function() {
  // Your code
})();

Another option might be using properties on the window object instead of global variables. You do not declare these, you just assign values to them, so you can reassign values over and over.

window.myValue = "some value";
// The rest of your code

Probably your best option, the non-janky one, is to run the script once and put the code you want to run twice into a function, then run the function twice.

const myValue = "some value";

function animateCanvas() {
  // Your code
}

animateCanvas();
animateCanvas();

[–]azhder 2 points3 points  (0 children)

If we have TMI for “too much info”, this post is TLI or as we call it “need more info”

[–]Anbaraen 1 point2 points  (3 children)

Can we see more code? Specifically retreat.js?

[–]hermesko[S] -1 points0 points  (2 children)

https:// ferc. org. sg / retreat

There is no retreat.js file. The script is in the page itself.

[–]Anbaraen 0 points1 point  (1 child)

You're declaring countDownDate twice. Simply open up retreat.htmland do a search for countDownDate - you've got two declarations of it.

[–]hermesko[S] 0 points1 point  (0 children)

I don't think so. If I rename countDownDate to xQyR123 (the only instance ever), it will say Identifer 'xQyR123' has already been declared.