Home Web Front-end JS Tutorial javascript setTimeout() passes function parameters (including passing object parameters)_javascript skills

javascript setTimeout() passes function parameters (including passing object parameters)_javascript skills

May 16, 2016 pm 06:30 PM
javascript settimeout Function parameters

So, I searched online and used another writing method setTimeout("fun(" parameter ")", 1000), but it still didn't work. However, the above writing method is OK when passing the textarea of ​​the form form. For example, hml is like this:

Copy code The code is as follows:















In js I can write like this:
setTimeout("doAjax(document. sform.txtara.value )", 1000);
But once you want to pass other parameters or object parameters, you have to rewrite this function according to the methods provided on the Internet.
After research and practice, I have improved the method myself. You can refer to it. I only base it on my personal needs. I cannot guarantee other situations. Please give me your advice!
Copy code The code is as follows:
















JavaScript:
Copy code The code is as follows:

function initAjax() {
var httprequest=null;
try {
httprequest =new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
try {
httprequest=new XMLHttpRequest();
}
catch (e) {
httprequest=null;
}
}
}
return httprequest;
}
function doAjax( msg, obj ) {
var obj=obj; // Mainly this line
alert( obj.value);
var he="he=" msg;
var ajaxrequest=initAjax();
ajaxrequest.open("POST", "abc.jsp", true);
ajaxrequest.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded;charset=utf-8");
ajaxrequest.send(he);
ajaxrequest.onreadystatechange=function() {
if (ajaxrequest.readyState= =4) {
if (ajaxrequest.status==200) {
document.getElementById("showpane").innerHTML=ajaxrequest.responseText;
}
else {
doAjax( msg );
}
}
}
setTimeout("doAjax(document.sform.txtara.value,document.all[" obj.sourceIndex "])", 100);//Also This line
}

In this way, I solved the problem of object parameter passing. Finally, I would like to say that if you have a better solution, please leave a comment. I am happy to cooperate with like-minded people. study!
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)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

The relationship between C++ function parameter passing methods and thread safety The relationship between C++ function parameter passing methods and thread safety Apr 12, 2024 pm 12:09 PM

Function parameter passing methods and thread safety: Value passing: Create a copy of the parameter without affecting the original value, which is usually thread safe. Pass by reference: Passing the address, allowing modification of the original value, usually not thread-safe. Pointer passing: Passing a pointer to an address is similar to passing by reference and is usually not thread-safe. In multi-threaded programs, reference and pointer passing should be used with caution, and measures should be taken to prevent data races.

Detailed explanation of C++ function parameters: implementation methods, advantages and disadvantages of indefinite parameter passing Detailed explanation of C++ function parameters: implementation methods, advantages and disadvantages of indefinite parameter passing Apr 28, 2024 am 09:48 AM

C++ indefinite parameter passing: implemented through the... operator, which accepts any number of additional parameters. The advantages include flexibility, scalability, and simplified code. The disadvantages include performance overhead, debugging difficulties, and type safety. Common practical examples include printf() and std::cout, which use va_list to handle a variable number of parameters.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Detailed explanation of C++ function parameters: the essence and precautions of the outgoing mechanism Detailed explanation of C++ function parameters: the essence and precautions of the outgoing mechanism Apr 27, 2024 pm 12:00 PM

There are two ways to pass function parameters in C++: call by value (which does not affect the actual parameters) and call by reference (which affects the actual parameters). Passing out parameters is achieved by passing a reference or pointer, and the function can pass the value to the caller by modifying the variable pointed to by the parameter reference or pointer. Please note when using: The outgoing parameters must be clearly declared, can only correspond to one actual parameter, and cannot point to local variables within the function. When calling by passing a pointer, be careful to avoid wild pointers.

Can arrays be used as function parameters? Can arrays be used as function parameters? Jun 04, 2024 pm 04:30 PM

Yes, in many programming languages, arrays can be used as function parameters, and the function will perform operations on the data stored in it. For example, the printArray function in C++ can print the elements in an array, while the printArray function in Python can traverse the array and print its elements. Modifications made to the array by these functions are also reflected in the original array in the calling function.

See all articles