当前位置:网站首页>SetTimeout parameters [easy to understand]
SetTimeout parameters [easy to understand]
2022-07-25 01:42:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
unction test(s)
{
alert(s);
}
window.setTimeout(function(){test('str');},1000);That's it … Why is that so . because setTimeout The first parameter required is the function … such as setTimeout(a_fun,1000) This is OK ,a_fun It's a function . however setTimeout(a_fun(),1000); That won't work . Because here a_fun() It's actually the return value of the function … It should be clear in this way . in addition , It is not recommended that someone use the method on the Internet setTimeout(‘test(1)’,1000); Form like this , Because this is not practical in many cases . If you really want to use this method , Only such :setTimeout(“test(‘”+param+”‘)”,100); This way, .
There's another way :
function timeClick(obj)
{
// var obj=document.getElementById('');
obj.style.backgroundColor="gray";
obj.onclick=function(){ return false;};
setTimeout(function(){resetbgColor(obj);},3000);
}
function resetbgColor(obj)
{
// var obj=document.getElementById('');
obj.style.backgroundColor="";
// $('#'+'').click(timeClick);
// debugger;
obj.οnclick=function(){
timeClick(this);
};
} I find this sentence is really omnipotent , I'm so impressed , function(){ method(param);}
Both of these places are used , Solved all my problems ,1.setTimeout The parameter 2. Dynamic setting onclick event
setTimeout(function(){resetbgColor(obj);},3000);
obj.οnclick=function(){ timeClick(this); };
There's another way :
<script language="JavaScript" type="text/javascript">
<!--
var userName="jack";
// Display welcome information according to user name
function hello(_name){
alert("hello,"+_name);
}
// Create a function , Used to return a parameterless function
function _hello(_name){
return function(){
hello(_name);
}
}
window.setTimeout(_hello(userName),3000);
//-->
</script>this A function is defined in _hello, Used to receive a parameter , And return a function without parameters , The parameters of the external function are used inside this function , So as to call , You don't need to use parameters . stay window.setTimeout Function , Use _hello(userName) To return a function handle without parameters , Thus, the function of parameter transmission is realized .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/108556.html Link to the original text :https://javaforall.cn
边栏推荐
- Harbor installation
- EasyX realizes button effect
- Talk about resume optimization and interview skills of algorithm post!
- Human cell prosci 4-1BB ligand recombinant protein scheme
- Contemporary fairy quotations
- Opengauss kernel analysis: query rewriting
- Data management process model of science and technology planning project
- How to implement the server anti blackmail virus system is a problem we have to consider
- Example analysis of recombinant monoclonal antibody prosci CD154 antibody
- 1260. Two dimensional grid migration: simple construction simulation problem
猜你喜欢

What are the important trends revealed by the release of "operator data viability index"?

Upgrade the leapfrog products again, and the 2023 Geely Xingrui will be sold from 113700 yuan

Kernel structure and design

How to obtain workers' coats and helmets in stray? How to obtain workers' helmets

VC hesitates to invest in Henan

Thinkphp5.0.24 deserialization chain analysis

Several schemes of traffic exposure in kubernetes cluster

MySQL series | log module

After burning up 130 billion yuan in ten years, vertical e-commerce will eventually enter the dust of history

What does it operation and maintenance management mean? How to establish an effective IT operation and maintenance management system?
随机推荐
Interview questions
How can arm access the Internet through a laptop?
Antdb database products were selected into the global database industry map (2022) of the China Academy of communications and communications
Young people who lost the IPO
MySQL series | log module
Shell judges whether the file exists and whether the file size is 0
Common functional interfaces_ Predicate interface_ Default method and and predicate interface exercise_ Set interface filtering
Origin and development of philosophy Hegel external historical form
The introduction of 23 Filipino doctors for 18million was a hot topic, and the school teacher responded: expedient
C traps and defects Chapter 2 lexical "traps" 2.4 switch statements
Prosci 14-3-3 (phosphate ser58) antibody instructions
Start to build a three node Eureka cluster
How to implement the server anti blackmail virus system is a problem we have to consider
Custom type
Point to point copy and paste of web pages
Inventory of well-known source code mall systems at home and abroad
Musk responded whether he would upload his brain to the cloud: already did it!
[26. String hash]
[summer daily question] Luogu P7550 [coci2020-2021 6] bold
Rightmost × Microframe, high quality heif image coding and compression technology