当前位置:网站首页>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
边栏推荐
- Password input box and coupon and custom soft keyboard
- Introduction to ORM framework - what is ORM framework?
- Several application scenarios of NAT
- SAP Spartacus - progressive web applications, progressive web applications
- Visual studio code installation package download slow & Installation & environment configuration & new one-stop explanation
- Management mode of agricultural science data center based on life cycle theory
- [summer daily question] output of Luogu p1157 combination
- JS convert pseudo array to array
- Three possible scenarios for SAP Spartacus server-side rendering
- Pursue and kill "wallet Assassin" all over the network
猜你喜欢

Luo min cannot become Dong Yuhui

Alibaba cloud released the white paper "upgrade - standardization of data warehouse upgrade delivery"

Take the first place in the International Olympic Games in mathematics, physics and chemistry, and win all the gold medals. Netizen: the Chinese team is too good

Fabric. JS centered element

Interpretation of video generation paper of fed shot video to video (neurips 2019)

Redis learning notes (2) - power node of station B

The two supply chain centers of HEMA launched the "background" of innovative research and development of multi format commodities

Three modes of executing programs, memory and cache
![[hero planet July training leetcode problem solving daily] 20th BST](/img/25/2d2a05374b0cf85cf123f408c48fe2.png)
[hero planet July training leetcode problem solving daily] 20th BST

JVM Foundation
随机推荐
MySQL series | log module
[programmer interview classic] 01.09 string rotation
Leetcode - number of palindromes
Target segmentation for 10000 frames of video, less than 1.4GB of video memory, open source code | ECCV 2022
Open sharing of scientific data in the context of open science: the practice of the national Qinghai Tibet Plateau scientific data center
Prosci anti-CD22 antibody epratuzum28 flow cytometry display
Three possible scenarios for SAP Spartacus server-side rendering
Performance analysis method - Notes on top of performance
Worthington cytochrome c digestion study carboxypeptidase B scheme
Contemporary fairy quotations
Summary of the most complete MySQL data types in history (Part 2)
The introduction of 23 Filipino doctors for 18million was a hot topic, and the school teacher responded: expedient
Kubernetes creates a user with dashboard read-only permission (with exec permission)
[basic usage of STL]
Ecosystem long-term observation data product system
The importance of system to Philosophy Hegel
How to use ES6 async and await (basic)
Open source demo | release of open source example of arcall applet
Listing of China graphite: the market value is nearly HK $1.2 billion, achieving a zero breakthrough in the listing of Hegang private enterprises
Several application scenarios of NAT