简体   繁体   中英

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. 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.

You can use the setTimeout method to execute some javascript function after a certain period of time.

So basically when your first button is clicked, start a timer using setTimeout and programatically click the second button using the trigger jQuery method.

For example, the below code will programatically click the second button after 5 seconds.

$(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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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