


Customize a timer in JS to execute it at a certain moment_javascript tips
Sometimes, due to demand reasons, we need to write a method in JS and then let it be executed at a certain moment. That is, we need to write a timer in JS. When the time reaches the required time, the method that needs to be executed automatically Execution, let me briefly talk about how I implemented it
var tMinutes=0; var tHours=0; var go; function dingshi(hours,minutes){ tHours = hours; tMinutes = minutes; go=setInterval(run,3000); } function run(){ var date=new Date(); if((date.getMinutes()-tMinutes==0) &&(date.getHours()-tHours==0)){ clearInterval(go); getData(); //要执行的方法 } } }
The parameters hours and minutes in dingshi are the start time of the method to be executed. Here we only require hours and minutes. Under specific circumstances, you can add parameters by yourself, but be careful to modify the judgment conditions in the if in the run method.
getData is the method to be executed, and it can be modified according to the actual situation. Just call the dingshi method when using it.
Also note that in order to prevent the browser from crashing, I set the second parameter of setInterval to 3000 milliseconds, which is 3 seconds. If your timing requirements are accurate to seconds, you should change it to 1000, otherwise you may be missed. set time.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How long can you set a timer on your iPhone camera? When you access the timer options in the iPhone's camera app, you'll be given the option to choose between two modes: 3 seconds (3s) and 10 seconds (10s). The first option lets you take a quick selfie from the front or rear camera while you're holding your iPhone. The second option is useful in scenes where you can mount your iPhone on a tripod from a distance to click group photos or selfies. How to Set a Timer on an iPhone Camera While setting a timer on an iPhone camera is a fairly simple process, exactly how to do it varies depending on the iPhone model you're using.

The timer expression is used to define the execution plan of the task. The timer expression is based on the model of "execute a task after a given time interval". The expression usually consists of two parts: an initial delay and a time interval.

How to implement the timer function in the Workerman document Workerman is a powerful PHP asynchronous network communication framework that provides a wealth of functions, including the timer function. Use timers to execute code within specified time intervals, which is very suitable for application scenarios such as scheduled tasks and polling. Next, I will introduce in detail how to implement the timer function in Workerman and provide specific code examples. Step 1: Install Workerman First, we need to install Worker

Java timer: How to set a scheduled execution task every day? In daily Java development, we often encounter the need to perform a certain task regularly every day. For example, perform a data backup task at 1 a.m. every day, or send a daily email at 8 p.m., etc. So in Java, we can use timers to achieve such a function. Java provides a variety of timer implementation methods. This article will introduce two methods based on Timer and ScheduledExecutorService.

The working principle of timers can be divided into two types: hardware timers and software timers. The working principle of the hardware timer is that the clock signal source provides a stable clock signal as the reference of the timer. The counter starts counting from a preset value and is incremented every time the clock signal arrives. When the counter reaches the preset value, the timer will trigger an interrupt signal to notify the interrupt controller to process the corresponding interrupt service routine. In the interrupt service routine, some predetermined operations can be performed. The working principle of the software timer is implemented through library functions or system calls provided by the programming language or system, etc.

Java timer: How to set a monthly scheduled execution task? Introduction: In development, we often encounter scenarios that require monthly execution of tasks, such as monthly update of statistical data, regular sending of reports, etc. Java provides a variety of timer implementation methods. This article will introduce how to use Java timers to implement monthly scheduled execution tasks and provide specific code examples. 1. Use the Timer class to implement monthly scheduled tasks. The Timer class is the most basic timer class provided by Java, through which simple scheduled tasks can be implemented.

Master the time.NewTimer function in the Go language documentation to implement a one-shot timer, and attach specific code examples. Time is the benchmark of our lives, and timers are one of the most commonly used tools in programming. In the Go language, we can use the time package to handle time-related operations, and the NewTimer function can be used to create a one-shot timer. This article will introduce how to use the NewTimer function to implement a simple one-shot timer, and attach specific code examples. In Go language, tim

Phalcon middleware: the ability to add scheduled tasks and timers to applications Introduction: When developing web applications, we often encounter the need to perform certain tasks regularly or perform a certain function within a specific time interval. As a high-performance PHP framework, Phalcon provides a flexible way to implement these functions, which is to add scheduled tasks and timers through middleware. 1. Introduction to Phalcon middleware Phalcon middleware is a tool that can be used to process HTTP requests.
