简体   繁体   English

一段时间后如何执行一个功能

[英]How to execute a function after some time

I want to write a javascript code where I can execute a function after exactly 30 minutes. 我想写一个javascript代码,我可以在30分钟后执行一个函数。

say I have a function called getScore and another function called getResult . 说我有一个名为getScore的函数和另一个名为getResult函数。 I want those functions to be executed after exactly thirty minutes. 我想要在三十分钟后执行这些功能。

It's for a quiz purpose, the quiz duration is thirty minutes, so after the time passes, both functions should be executed. 这是一个测验目的,测验持续时间是三十分钟,所以经过一段时间后,两个函数都应该执行。

You should use setTimeout() : 你应该使用setTimeout()

setTimeout(function() {
    getScore(); 
    getResult(); 
}, 1800000);

The '1800000' is the time in milliseconds after which you want this function to execute. '1800000'是您希望此功能执行的时间(以毫秒为单位)。 In this case, 30 minutes. 在这种情况下,30分钟。

If You Want To Show Element ,you can use This Code , This Will Show Button. 如果您想要显示元素,您可以使用此代码,这将显示按钮。

     function myFunc(){
        var linkEd = document.getElementById("buttonID");
        var timing = 6;
        var text_timing = document.createElement("span");
        linkEd.parentNode.replaceChild(text_timing, linkEd);
        var id;
        id = setInterval(function() {timing--;
      if (timing < 0) {
        clearInterval(id);
       text_timing.parentNode.replaceChild(linkEd, text_timing);
      } else {
       text_timing.innerHTML = "<h3 class='yourclass'>Button will appear after  <strong>" + timing.toString() + "</strong>  second</h3>";
      linkEd.style.display = "inline";
      }
    }, 1000);
}

This Is To Showing Time miliseconds 这是显示时间毫秒

<h3 class='yourclass'>Button will appear after <strong>" + timing.toString() + " </strong> second</h3> <h3 class ='yourclass'>按钮将出现在<strong>“+ timing.toString()+”</ strong> second </ h3>之后


To Call Time And show Button Use onclick event to show button after time 0 呼叫时间并显示按钮使用onclick事件在时间0之后显示按钮

<button class='yourclass' onclick='myFunc()' id='button'>show time</button> <button class ='yourclass'onclick ='myFunc()'id ='button'> show time </ button>

use display none to hide the button 使用display none隐藏按钮

<button style='display:none' id='buttonID' class='yourclass'>after time</button> <button style ='display:none'id ='buttonID'class ='yourclass'>在时间</ button>之后

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

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