Sekoul
Sekoul

Reputation: 1371

Javascript - one function's variables interfere with another

I'm using three.js to display a 360 image on this page. However, the first section of the page (scroll to top, or follow this link), had another 360 image which used to work the same way, and now does not.

On mobile, both work fine (a different script is used on mobile devices), but on desktop, the 2nd seems to interfere with the first somehow.

Upvotes: 0

Views: 87

Answers (1)

Kevan Stannard
Kevan Stannard

Reputation: 1067

After a quick look it appears that your mobile code is enclosed in a self executing function:

(function() {
    // Mobile code here
})();

but your non-mobile code is not, which means that variables with the same name all go into the global window scope and overwrite each other.

This might explain why your mobile code works and non-mobile code does not work.

Try enclosing your non-mobile code in a self-executing function as well and see if that helps.

I suspect this is not a three.js question, but maybe just a JavaScript question.

Upvotes: 2

Related Questions