简体   繁体   English

“prompt()”没有记录它对 setInterval 函数的回答

[英]"prompt()" not recording it's answer for a setInterval function

EDIT:I have decided to make a manual refresh button rather than have it auto refresh编辑:我决定制作一个手动刷新按钮而不是让它自动刷新

I am trying to create a live chat system using google sheets and google forms, I thought I would add a way to change the refresh rate, so I added a button to change the amount of time in seconds between refreshes, but what I enter in the prompt doesn't seem to change the refresh rate.我正在尝试使用 google 表格和 google 表单创建一个实时聊天系统,我想我会添加一种更改刷新率的方法,所以我添加了一个按钮来更改刷新之间的时间(以秒为单位),但是我输入的内容提示似乎没有改变刷新率。 Any help is greatly appreciated!!!任何帮助是极大的赞赏!!! (also note that "urlforgooglesheets" or "urlforgoogleform" are not the actual URLs I have entered, I just put them like that for the code shown below) (另请注意,“urlforgooglesheets”或“urlforgoogleform”不是我输入的实际网址,我只是将它们放在下面显示的代码中)

 window.onload = function refresh() { var sheet = document.getElementById("formSheet"); var refreshRate = document.getElementById("refreshButton"); refreshRate.addEventListener("click", function() { var waitPrompt = prompt("After how many seconds should chat refresh?"); }); setInterval(function() { var waitTime = waitPrompt * 1000; sheet.src = "#"; setTimeout(function() { sheet.outerHTML = '<iframe id="formSheet" src="urlforthegooglesheets" height="550" width="453" frameborder="0"></iframe>'; }, 10); }, waitTime); }
 <button id="refreshButton">Change Refresh Rate</button> <iframe src="urlforgoogleform" width="525" height="580" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe> <iframe id="formSheet" src="urlforthegooglesheet" height="580" width="453" frameborder="0"></iframe>

Reading the error code, you can see that on line 13 of your JS codeblock, waitTime is not defined.阅读错误代码,您可以看到在您的 JS 代码块的第 13 行,没有定义waitTime This is because you define waitTime inside of the setInterval callback.这是因为您在 setInterval 回调中定义了waitTime You will also want to store the number returned by setInterval() as a global variable so that you can change the waitTime , and to change that you need to clearInterval() to prevent multiple intervals going on at once.您还需要将setInterval()返回的数字存储为全局变量,以便您可以更改waitTime ,并更改您需要clearInterval()以防止同时进行多个间隔。 I'm also going to put the default waiting as a variable at the top, you should remove this and put whatever you want as the default.我还将将默认等待作为变量放在顶部,您应该删除它并将您想要的任何内容作为默认值。

var waitInterval;
var defaultWait = 1;
window.onload = function refresh() {
  var sheet = document.getElementById("formSheet");
  var refreshRate = document.getElementById("refreshButton");
  refreshRate.addEventListener("click", function() {
    var waitPrompt = prompt("After how many seconds should chat refresh?");
    clearInterval(waitInterval);
    var waitTime = waitPrompt * 1000;
    waitInterval = setInterval(function() {
      sheet.src = "#";
      setTimeout(function() {
        sheet.outerHTML = '<iframe id="formSheet" src="urlforthegooglesheets" height="550" width="453" frameborder="0"></iframe>';
      }, 10);
    }, waitTime);
  });
  var waitTime = defaultWait * 1000;
  waitInterval = setInterval(function() {
    sheet.src = "#";
    setTimeout(function() {
      sheet.outerHTML = '<iframe id="formSheet" src="urlforthegooglesheets" height="550" width="453" frameborder="0"></iframe>';
    }, 10);
  }, waitTime);
}

This code doesn't show errors, but I can't test this with Google Sheets to know if it works.此代码未显示错误,但我无法使用 Google 表格对其进行测试以了解它是否有效。 Another thing to do is move the waitInterval = setInterval(...) into its own seperate function, but that's not necessary.另一件事是将waitInterval = setInterval(...)到它自己的单独函数中,但这不是必需的。

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

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