1

I have developed an MVC application and want to make my button click automatic. In this I want my button to click daily at 10 p.m. automatically. This is my button in my View.

 <a href="#" class="thumbnail">
<input type="submit" class="btn btn-default" id="btnAdd2" value="Add 2" />    
 </a>

I have googled a lot and got one such thing.

setInterval(click,3000);
        function click() {
            $("#btnAdd2").click();
            alert("Automatic button click after 3 seconds.");
        }

This is working fine but it is clicking after every 3 seconds. But I want it to click daily at 10 p.m. If anyone has idea to resolve this please help and your help would be appreciated.....Thanks.

19
  • 1
    But what if there is no user interacting with your site at 10 p.m.? I think that this is an XY Problem.
    – Steve
    Commented Apr 13, 2021 at 7:25
  • 1
    setInterval just tells the job to run repeatedly. Within that job you need to check the current time, and decide what to do. There's no way to trigger interval to run only at a specific time, you just need to let it repeat, and check the time of day repeatedly until you find it matches your target time.
    – ADyson
    Commented Apr 13, 2021 at 7:46
  • 2
    I want to update the database daily at 10 pm...if you want this to run consistently, every day, without user intervention, and always run even if no users are using the website at that time, then this would be far better implemented as a background task - e.g. using a windows service or scheduled task. Then it won't fail if the user isn't logged in, or is viewing another browser tab (which can cause scripts in the hidden tabs to stop running).
    – ADyson
    Commented Apr 13, 2021 at 7:48
  • 1
    @Steve I know. I saw your comment and agree with it. That's why I'm trying to prompt them to explain (I've asked at least twice), and suggesting a common alternative (with clear description of the scenario in which it would be used) in case that causes a lightbulb moment. :-)
    – ADyson
    Commented Apr 13, 2021 at 7:58
  • 1
    jQuery is the wrong way to approach this. It's the task of updating the DB that you want to automate, not the button click. Don't think in terms of the UI, think in terms of the task you want to accomplish. In your mind (and in your code!), separate the job itself from the user interface which triggers the job. As I mentioned earlier, this is a task you should do with a Windows service, or a Windows scheduled task which runs a separate small program to execute the database update. Then it's fully automatic and reliable and doesn't rely on a browser being open and visible in someone's screen.
    – ADyson
    Commented Apr 13, 2021 at 8:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.