当前位置:网站首页>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
边栏推荐
- Web Security Foundation - file upload
- The introduction of 23 Filipino doctors for 18million was a hot topic, and the school teacher responded: expedient
- Fabric. JS centered element
- Data governance notes
- Document the use of anti shake in packaged components and projects
- How to obtain workers' coats and helmets in stray? How to obtain workers' helmets
- Prosci anti-CD22 antibody epratuzum28 flow cytometry display
- Data integration | what are the tools for data integration at home and abroad?
- Kubernetes creates a user with dashboard read-only permission (with exec permission)
- Download files and web pages with WGet
猜你喜欢
10 commonly used data visualization tool software

Fraud detection using CSP

From casting sword to defending sword: the way to build the efficiency platform of didi project

Three modes of executing programs, memory and cache

Service address dynamic awareness of Nacos registry

MySQL series | log module

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

Data integration | what are the tools for data integration at home and abroad?
![[27. Expression evaluation (infix expression)]](/img/af/cf3c4a441232caeea9f9927ebdf87b.png)
[27. Expression evaluation (infix expression)]

Speed comparison between 64 bit width and 32 bit width of arm64 memory
随机推荐
Interpretation of video generation paper of fed shot video to video (neurips 2019)
Academicians said: researchers should also support their families. They can only do short-term and fast research if they are not promoted
Install scoop and lux (formerly Annie)
Take C language from 0 to 1 - program structure and use examples
Grpc sets connection lifetime and server health check
On let variable promotion
The IPO of Tuba rabbit was terminated: the annual profit fell by 33%, and Jingwei Sequoia was the shareholder
Antdb database products were selected into the global database industry map (2022) of the China Academy of communications and communications
record
WhatsApp web for usability testing of software testing technology
2022/7/18-7/19
Commonjs export import
MySQL series | log module
[28. Maximum XOR pair]
Leetcode - number of palindromes
Resolution of multi thread conflict lock
Worthington carboxyl transfer carbonic anhydrase application and literature reference
iptables :chains, target
Latex notes
[development tutorial 10] crazy shell · open source Bluetooth smart health watch OTA image production and download technical documents