window.variable can't be accessed

I am trying to make a website where there is the text "game over" with the id "vid" and I want it to randomize the uppercase and lowercase every 300 - 400 milliseconds and to remain like that for 40 milliseconds before resetting back to lowercase. This is the code I got now: (My Javascript is not very good)

setInterval(function() {
  window.smth = Math.floor((Math.random() * 100) + 1) + 300;
  var elem = document.getElementById('vid');
  elem.textContent = "game over";
  setTimeout(function() {
    elem.textContent = elem.textContent.split('').map(function(v) {
      var chance = Math.round(Math.random());
      return v = chance ? v.toUpperCase() : v.toLowerCase();
    }).join('');
  }, window.smth);
}, window.smth + 40);
<div id="vid"></div>

Because window.smth is set in the callback, it's undefined when setting the interval. Even if you define smth after the first time the callback is called, it is still not going to work because the setInterval function is only called once.

The way to create an interval like this is multiple timeouts; pseudocode:

setTimeout(_ => {
  // do something

  setTimeout(..., otherTime) // you don't want to nest and nest and nest this, just create a function for this that's recursive
}, time)

Global object, global variables of your script are in fact properties of window : var global = {data: 0}; alert(global === window.global); // displays "true"; you can� Use the Variables window to create and modify user-defined variables and view system variables. By default, the Variables window is located below the Connection Managers area in the SSIS Designer, in SQL Server Data Tools (SSDT). If you don't see the Variables window, click Variables on the SSIS menu to display the window.

You're declaring window.smith inside your first timeout callback, but you're using it BEFORE the callback is actually called. So window.smith at last row is actually still undefined at that time

Try moving window.smth definition out of first callback like this:

window.smth = Math.floor((Math.random() * 100) + 1) + 300;
setInterval(function() {
  var elem = document.getElementById('vid');
  elem.textContent = "game over";
  setTimeout(function() {
    elem.textContent = elem.textContent.split('').map(function(v) {
      var chance = Math.round(Math.random());
      return v = chance ? v.toUpperCase() : v.toLowerCase();
    }).join('');
  }, window.smth);
}, window.smth + 40);

Window.window, Why the heck would you do this? There are two reasons, and neither are huge. First, you can alias global variables. At the bottom, we reference� Under the Environment Variables window, choose or highlight the PATH variable in the System Variables section shown in the window. After highlighting the PATH variable from System Variables click the Edit button. You can add or modify the path lines with the directories you want your computer to look in for executable files.

Put smth out of the setInterval() and into another:

var smth = 500;
setInterval(function() {smth = Math.floor((Math.random() * 100) + 1) + 300;}, 250);

setInterval(function() {
  var elem = document.getElementById('vid');
  elem.textContent = "game over";
  setTimeout(function() {
    elem.textContent = elem.textContent.split('').map(function(v) {
      var chance = Math.round(Math.random());
      return v = chance ? v.toUpperCase() : v.toLowerCase();
    }).join('');
  }, smth);
}, smth);
<div id="vid"></div>

The window Object & Global Variables > JavaScript for PHP Geeks , A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function. Let's see the simple example� How-to. How-to: Windows Environment Variables. Environment variables are mainly used within batch files, they can be created, modified and deleted for a session using the SETcommand. To make permanent changes, use SETX. Variables can be displayed using either SETor ECHO.

JavaScript global variable, Since the variable can be accessed anywhere in the program, they can What's the difference between a global var and a window.variable in� Variables that are processed for the operating system and in the context of each user. You can use these variables within sections in the .xml files with context=UserAndSystem, context=User, and context=System. Same as CSIDL_COMMON_APPDATA. Refers to % PROFILESFOLDER %\Public or % PROFILESFOLDER %\all users.

JavaScript Scope, Node.js does not have a global variable window. Therefore, the You can, however store a reference to it in a variable, via an IIFE [1]. (function� Variable Object. The Variable object represents an HTML <var> element. Access a Variable Object. You can access a <var> element by using getElementById():

Global Variables in JavaScript Explained, You can access the variable with “window.VAR_NAME” or directly “VAR_NAME”; Do not use other variables with same name otherwise it may cause unwanted� As you can see, Node.js and Git already added their paths so that I can run Git commands and Node.js commands from anywhere while in the command prompt. To add a new path, simply click on New and it’ll add a new line to the bottom of the list. If you know the path, simply type it in or copy and paste it.

Comments
  • Don't use the window object as a global variable holder. Just assign a variable normally the times you want your interval to run, outside of the interval.
  • What actually is the problem here? Why do you think window.smth is ignored?
  • If you nest and nest and nest, you won't do it an infinite amount of times, you'll need to use setInterval()
  • Nope, that's not true. What i said in the comments; you'd have to create a function for this and call that recursively
  • That'll mean that window.smth is always the same.