简体   繁体   English

MVC 4在特定时间段后将单击该按钮

[英]MVC 4 the button will be clicked after a specific period of time

I'm working with MVC 4 razor and I have a problem. 我正在使用MVC 4剃须刀,但遇到了问题。 I couldn't find anything in web the same as my problem. 我在网络上找不到与我的问题相同的任何东西。 I don't know how do I code it. 我不知道该如何编码。

Example: After clicking the first button at 6:00 am, the second button will automatically be clicked after 7:00 am or clicking the first button at 6:30 am, the second button will still automatically be clicked after 7:00 am. 示例:在上午6:00单击第一个按钮后,在上午7:00之后将自动单击第二个按钮,或者在上午6:30时单击第一个按钮,在上午7:00之后仍将自动单击第二个按钮。

You can use the setTimeout method to execute some javascript function after a certain period of time. 您可以在一段时间后使用setTimeout方法执行一些javascript函数。

So basically when your first button is clicked, start a timer using setTimeout and programatically click the second button using the trigger jQuery method. 因此,基本上,当单击第一个按钮时,请使用setTimeout启动计时器,并使用trigger jQuery方法以编程方式单击第二个按钮。

For example, the below code will programatically click the second button after 5 seconds. 例如,以下代码将在5秒钟后以编程方式单击第二个按钮。

$(document).ready(function() {

    $("#button1").click(function(e) {
        e.preventDefault();
        alert('first button clicked');
        var delayDuration = 5000;  // 5 seconds
        var j = window.setTimeout(function() {
                 console.log("going to trigger click on button 2");
                 $("#button2").trigger( "click" );
            },
            delayDuration);

    });

    $("#button2").click(function(e) {
        e.preventDefault();
        alert('second button clicked');
    });

});

Keep in mind that, this is client side javascript code. 请记住,这是客户端javascript代码。 that means user need to have his browser/page open for that much time (1 hour/30 minutes or whatever the delay duration is) for it to work. 这意味着用户需要长时间打开浏览器/页面才能正常工作(1小时/ 30分钟或任何延迟时间)。

If you are trying to do something automatic (Without the browser being open), you should use some sort of task scheduling mechanism in the server. 如果您尝试自动执行某项操作(未打开浏览器),则应在服务器中使用某种任务调度机制。

Here is a working jsbin 是一个工作的jsbin

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

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