简体   繁体   English

window.variable 无法访问

[英]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.我正在尝试创建一个网站,其中包含 ID 为“vid”的文本“游戏结束”,我希望它每 300 - 400 毫秒随机化大写和小写,并在重置回小写之前保持 40 毫秒. This is the code I got now: (My Javascript is not very good)这是我现在得到的代码:(我的Javascript不是很好)

 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>

Everything works fine except the last 2 lines ignore "window.smth".除了最后两行忽略“window.smth”外,一切正常。 I tried different ways of setting "smth" but I couldn't find a way that loops with the other code and can be accessed everywhere I use it.我尝试了不同的设置“smth”的方法,但我找不到一种与其他代码循环并且可以在我使用它的任何地方访问的方法。

Because window.smth is set in the callback, it's undefined when setting the interval.因为window.smth是在回调中设置的,所以在设置interval的时候是undefined 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.即使您在第一次调用回调后定义smth ,它仍然不会工作,因为setInterval函数只被调用一次。

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)

You're declaring window.smith inside your first timeout callback, but you're using it BEFORE the callback is actually called.您在第一个超时回调中声明window.smith ,但在实际调用回调之前使用它。 So window.smith at last row is actually still undefined at that time所以最后一行的window.smith当时实际上还undefined

Try moving window.smth definition out of first callback like this:尝试将window.smth定义从第一个回调中移出,如下所示:

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);

Put smth out of the setInterval() and into another:smthsetInterval()放入另一个:

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM