Home Web Front-end JS Tutorial Javascript timer calls the method of passing parameters_javascript skills

Javascript timer calls the method of passing parameters_javascript skills

May 16, 2016 pm 06:41 PM
Pass parameters timer

Whether it is window.setTimeout or window.setInterval, you cannot take parameters when using the function name as the calling handle. In many cases, you must take parameters, so you need to find a way to solve it.
For example, for the function hello(_name), It is used to display the welcome message for the username:

Copy the code The code is as follows:

var userName ="Tony";
//Display welcome message based on user name
function hello(_name){
alert("hello," _name);
}

At this time, it is not feasible to use the following statement to delay the execution of the hello function for 3 seconds:
window.setTimeout(hello(userName),3000);
This will cause the hello function to execute immediately and return The value is passed to the setTimeout function as a call handle, and the result is not what the program needs. The desired result can be achieved by using the string form:
window.setTimeout("hello(userName)",3000);
here The string is a piece of JavaScript code, in which userName represents a variable. However, this way of writing is not intuitive enough, and in some cases function names must be used. Here is a little trick to call a function with parameters:
Copy code The code is as follows:



A function _hello is defined here to receive a parameter and Returns a function without parameters. The parameters of the external function are used inside this function, so that it can be called without using parameters. In the window.setTimeout function, use _hello(userName) to return a function without parameters. handle, thus realizing the function of parameter passing.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set a timer on your iPhone camera How to set a timer on your iPhone camera Apr 14, 2023 am 10:43 AM

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.

What is java timer expression What is java timer expression Dec 27, 2023 pm 05:06 PM

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 How to implement the timer function in the Workerman document Nov 08, 2023 pm 05:06 PM

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

How to set a timer for daily scheduled task execution in Java? How to set a timer for daily scheduled task execution in Java? Dec 27, 2023 am 11:10 AM

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.

How does a timer work? How does a timer work? Aug 16, 2023 pm 02:18 PM

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.

How to set up scheduled monthly tasks in Java? How to set up scheduled monthly tasks in Java? Jan 11, 2024 pm 04:50 PM

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 Go language documentation to implement a single timer Master the time.NewTimer function in Go language documentation to implement a single timer Nov 03, 2023 pm 02:19 PM

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: Add the function of scheduled tasks and timers to applications Phalcon middleware: Add the function of scheduled tasks and timers to applications Jul 30, 2023 pm 06:08 PM

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.

See all articles