试图通过一个函数获得一个按钮来执行setTimeout

时间:2017-02-23 18:06:15

标签: javascript html

这是我第一次尝试使用setTimeout而我正在使用W3schools作为指导,但我必须在onclick行的某个地方出错,我正在尝试做的是将此功能延迟5秒。这是我的代码。任何帮助深表感谢。如果它在没有setTimeout

的情况下帮助函数按预期工作

function scanalertfunction(id) {
  var divelement1 = document.getElementById(id)

  document.getElementById("scanalertmsg").innerHTML = "Scan Failed! Please try again";
  divelement1.style.display == 'none'
  divelement1.style.display = 'block';
}
<div id="scanalert" style="display:none;">
  <span class="scanclosebtn" onclick="this.parentElement.style.display='none';">&times;</span>
  <p id="scanalertmsg"> </p>
</div>
<button onclick="setTimeout(scanalertfunction('scanalert'); 5000);">Ready</button>

2 个答案:

答案 0 :(得分:0)

语法错误很少

检查此代码段

&#13;
&#13;
function scanalertfunction(id) {
  var divelement1 = document.getElementById(id)

  document.getElementById("scanalertmsg").innerHTML = "Scan Failed! Please try again";
  divelement1.style.display == 'none'
  divelement1.style.display = 'block';
}
&#13;
<div id="scanalert" style="display:none;">
  <span class="scanclosebtn" onclick="this.parentElement.style.display='none';">&times;</span>
  <p id="scanalertmsg"> </p>
</div>
<button onclick="setTimeout(scanalertfunction('scanalert') ,5000);">Ready</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

正如cyrix所提到的,setTimeout需要一个函数作为其参数。请参阅下面的工作解决方案。

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 4, 1,
    TimeUnit.MINUTES, new LinkedBlockingQueue<>()) {
        @Override protected void afterExecute(Runnable r, Throwable t) {
            if(t!=null) synchronized(System.out) {
               System.out.println("Uncaught exception in "+Thread.currentThread().getId());
               t.printStackTrace(System.out);
            }
        }
    };
function scanalertfunction(id) {
  var divelement1 = document.getElementById(id)

  document.getElementById("scanalertmsg").innerHTML = "Scan Failed! Please try again";
  divelement1.style.display == 'none'
  divelement1.style.display = 'block';
}