JS settimeout parameter transfer using introduction _javascript skills

Source: Internet
Author: User
Tags rand
The Window.settimeout () method calls a function with parameters in two ways:
1.
Copy Code code as follows:

function init () {
var url = "<%=basepath%>fetchwater.do?method=searchrealwater&xzqh=" + "<%=xzqh%>" + "&rand=" + Math.random ();
alert (URL);
Window.settimeout (function () {searchjdwater (URL);},100);
}

2.
Copy Code code as follows:

function init () {
var url = "<%=basepath%>fetchwater.do?method=searchrealwater&xzqh=" + "<%=xzqh%>" + "&rand=" + Math.random ();
alert (URL);
Window.settimeout ("Searchjdwater" ("+url+") ", 100);
}

Note: With the parameter method use settimeout to notice, settimeout ("function name (" + parameter + ")", milliseconds, the parameter here can only be string form, and cannot pass an object
---------------------------
The following method is incorrect and the browser status bar prompts for an invalid parameter:
Copy Code code as follows:

function init () {
var url = "<%=basepath%>fetchwater.do?method=searchrealwater&xzqh=" + "<%=xzqh%>" + "&rand=" + Math.random ();
alert (URL);
Window.settimeout (Searchjdwater (URL), 100);
}

--------------------------------------------------------------
settimeout () function extension:
Many friends on the Internet are also asking such questions, I hereby explain below, I give a few simple examples:
Online lookup "with parameter settimeout", many friends wrote a lot of ways to implement the use of SetTimeout with objects of the method loop, for example:
Copy Code code as follows:

<script language= "JavaScript" >
var __sto = settimeout;
Window.settimeout = function (Callback,timeout,param)
{
var args = Array.prototype.slice.call (arguments,2);
var _CB = function ()
{
Callback.apply (Null,args);
}
__sto (_cb,timeout);
}
Test code
Function AAAA (a)
{
alert (a);
}
function AAAAA (a,b,c)
{
Alert (A + B + c);
}
var a = new Object ();
Window.settimeout (Aaaa,1000,a);
Window.settimeout (aaaaa,2000,a,6,7);
</script>

In this example, the settimeout usage, settimeout (callback function, time, parameter 1,..., parameter n).
Another example:
2:
Copy Code code as follows:

<script type= "Text/javascript" >
var _st = window.settimeout;
Window.settimeout = function (Fref, mdelay) {
if (typeof fref = = ' function ') {
var Argu = Array.prototype.slice.call (arguments,2);
var f = (function () {fref.apply (null, ARGU);});
Return _st (f, mdelay);
}
Return _st (Fref,mdelay);
}
function test (x) {
alert (x);
}
Window.settimeout (test,1000, ' Fason ');
</script>

In this case, overload the Window.settimeout and use apply to callback the previous function.
Example 1:
Copy Code code as follows:

<script language= "JavaScript" >
function test (obj)
{
alert (obj);
settimeout ("Test (" + obj + ")", 1000);
}
</script>
<input id= "Testbutton" type= "button" onclick= "Test (1)" >

When the mouse presses this button, call Test (), pass "1" into the screen every 1000 milliseconds (1 seconds) Pop-up dialog box, showing 1, no problem.
Example 2:
Copy Code code as follows:

<script language= "JavaScript" >
function test (obj)
{
alert (obj);
settimeout ("Test (" + obj + ")", 1000);
}
</script>
<input id= "btntest" type= "button" onclick= "Test (This)" >

This can be understood as document.getElementById ("btntest"), and when the mouse presses this button it passes an object to the test function, and the screen displays [object],
No longer displays the second time after 1000 milliseconds. The problem is, the browser left the next report script error, the details of the object is not defined.
Example 3:
Copy Code code as follows:

<script language= "JavaScript" >
function test (obj)
{
alert (obj);
settimeout ("Test ()", 1000);
}
</script>
<input id= "Testbutton" type= "button" onclick= "Test (This)" >

SetTimeout test () without parameters, the first display [object],1000 milliseconds after the undefined, the variable has not been assigned value, that is, in this way can be implemented with parameters of the party
Method Loop, but the parameters are destroyed.
In fact, this is very simple to implement, do not have to write so long code to achieve.
Example 4:
Copy Code code as follows:

<script language= "JavaScript" >
function test (obj)
{
alert (obj);
settimeout ("Test ('" + obj + ")", 1000);
}
</script>
<input id= "Testbutton" type= "button" onclick= "Test (This)" >

Notice that the quotation marks around the parameter obj in the test in settimeout are enclosed in double quotes with a single quotation mark, so that: settimeout ("Test" (single quote double quotation mark +obj+ double quote quote
), OK ~ Every 1000 milliseconds the screen is displayed once [object], passing the object successfully.
———————————————————————————————————————
Doubt: The 4th article eventually passes the string "object" instead of the original object obj. Just the type of obj. Does not achieve the effect of passing objects.
You can test the code: the ID of the object that pops up
Copy Code code as follows:

<body>
<div id= "sssss" ></div>
</body> ;
<script language= "JavaScript" >
Obj=document.getelementbyid (' sssss ');
function Test (obj)
{
alert (obj.id);
settimeout ("" Test (' + obj + ') ", 1000);
}
Test (obj)
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.