在自动填充 javascript 上编辑 settimeout

Posted

技术标签:

【中文标题】在自动填充 javascript 上编辑 settimeout【英文标题】:Edit settimeout on autofill javascript 【发布时间】:2015-11-23 17:57:46 【问题描述】:

我想放置一个命令,例如让我改变点击后的等待时间

var myVar;
    
function myFunction() 
  myVar = setTimeout(alertFunc, 3000);


function alertFunc() 
  alert("Hello!");
<p>Click the button to wait 3 seconds, then alert "Hello".</p>

<button onclick="myFunction()">Try it</button>

【问题讨论】:

你有什么问题? 【参考方案1】:

这可以通过 jQuery 轻松实现。

$(".my-button").on("click", function(e)
    e.preventDefault();
    setTimeout(function()
        alert("hello");
   , 3000);
)

【讨论】:

除非 OP 指定 jQuery,否则最好不要推荐它作为答案。【参考方案2】:

Plunkr Example with enhanced code

JS

// Code goes here

var myVar;

function myFunction() 
  // this is to make sure we don't spin up infinite timeouts and have zombie threads
  if(myVar === undefined) 
    myVar = setTimeout(alertFunc, 3000);
   else 
    alert("already running...");
  


function alertFunc() 
  alert("hello!");
  // clears the thread
  clearTimeout(myVar);
  myVar = undefined;


您的代码

您的代码似乎工作得很好,alert 不在堆栈溢出沙箱中时也可以工作,我唯一添加的是使用 clearTimeout 清除超时,这样它就不会无限地运行。

var myVar;
    
function myFunction() 
  myVar = setTimeout(alertFunc, 3000);


function alertFunc() 
  console.log("Hello!");
  clearTimeout(myVar);
<p>Click the button to wait 3 seconds, then alert "Hello".</p>

<button onclick="myFunction()">Try it</button>

【讨论】:

以上是关于在自动填充 javascript 上编辑 settimeout的主要内容,如果未能解决你的问题,请参考以下文章

在编辑模式下,Material-ui/DatePicker 中未填充日期

检测自动填充和停止对焦

如何使用下拉列表的不同选项自动填充编辑字段?

如何使用来自两个不同表的旧信息自动填充编辑表单?

修改VIM编辑器使其自动显示行号缩进

如何在 OOo Calc 中自动填充 OpenOffice Math 公式编辑器?