当前位置:网站首页>About JS console Log() is a problem caused by synchronous or asynchronous
About JS console Log() is a problem caused by synchronous or asynchronous
2022-06-09 02:53:00 【Know the white and keep the black_】
Find the problem
I'm learning 《Vue.js Design and implementation 》 In the renderer section , It was found that the virtual... Was modified after the rendering function was called DOM Value , What you get in the rendering function is a new virtual DOM.
const vnode = {
type: "div",
children: [
{
type: "p",
children: "text",
},
],
};
const app = document.querySelector("#app");
renderer.render(vnode, app);
vnode.children = [
{
type: "input",
props: {
value: " Please enter keywords ",
},
},
];
- Print the virtual in the render function DOM Value , The findings are

Change virtual DOM The code to put setTimeout Function
setTimeout(() => {
vnode.children = [
{
type: "input",
props: {
value: " Please enter keywords ",
},
},
];
});
It is found that the print result has not changed, and it is still a modified virtual DOM.
then setTimeout The callback time of is modified to 2000 ms, Just got the result I wanted .
From this, it can be inferred that , There is code in the rendering function that causes asynchronous operation .
Location problem
According to the rendering process , Found code in the code that could cause asynchrony :
- document.createElement(localName, options);
- insertBefore(node, child)
Then go Dom Standard Find the specific definitions of these two methods in :
stay createElement Step 6 of returns to creating an element Result 
So I went to check creating an element The definition of :
But there is no information about asynchronous operation found in the definition .
Then go and check insertBefore The detailed definition of , The result is the same , There is no evidence that insertBefore Asynchronous operation is involved .
Then synchronous operation leads to asynchronous result , Then the problem is probably not in the rendering function , So I use console.log() Print virtual DOM, Sure enough , Get the latest virtual DOM:
console.log(vnode);
setTimeout(() => {
vnode.children = [
{
type: "input",
props: {
value: " Please enter keywords ",
},
},
];
});
After checking some posts, I found that someone mentioned in 《 You don't know JavaScript Medium volume 》 The answer to the question in :
Asynchronous console
There is no specification or set of requirements to specify console.* How method families work —— They're not JavaScript A formal part , But by the host environment ( Please refer to “ Type and grammar ” part ) Add to JavaScript Medium . therefore , Different browsers and JavaScript The environment can be achieved as it wishes , Sometimes it causes confusion .
In particular , on certain conditions , Some browsers console.log(…) It doesn't output the incoming content immediately . The main reason for this is , In many programs ( It's not just JavaScript) in ,I/O It's a very slow blocking part . therefore ,( From page /UI From the perspective of ) The browser processes the console asynchronously in the background I/O Can improve performance , At this point, users may not even realize that it happened .
give an example 
At the same time, the book gives the solution :
If we encounter this rare situation , The best choice is JavaScript Use breakpoints in debugger , Don't rely on console output . The suboptimal solution is to serialize the object into a string , To enforce once “ snapshot ”, Such as through JSON.stringify(…).
Problem solving :
let vnodeStr = JSON.parse(JSON.stringify(vnode));
console.log(vnodeStr);

Reference material
- Dom Standard
- console.log Is an asynchronous flow ? I feel like I stepped on a pit
- 《 You don't know JavaScript Medium volume 》
边栏推荐
- Two important influencing factors of togglerowselection() failure
- Binding mode of QT overloaded signal slot function
- In unity, inherit the lifecycle of monobehavior game objects
- Leetcode 801. Minimum number of exchanges DP to increment the sequence
- Leetcode 871. Minimum refuelling times priority queue
- Ccf-csp 202109-3 pulse neural network, sometimes 100 points..
- RTSP/Onvif协议视频平台EasyNVR对静态文件大小的优化
- Coulometer scheme
- Blue Bridge Cup_ Diophantine equation
- Dynamic programming / memo method n's K splitting n's maximum addend K splitting
猜你喜欢

What is the network transformer for? (Ethernet network LAN LAN communication isolation filter) production plant / product schematic diagram / common products / price influencing factors
![[network protocol] | [01] network byte order big end and small end](/img/9f/c69293aa8983161f4733a84389a311.png)
[network protocol] | [01] network byte order big end and small end

Ccf-csp 201403-3 command line options

What does this SQL question mean

New textbook for self taught examination-p292

Implementation of hash table of Telephone Query System for C language project (project requirements + operation interface + code analysis + complete code)

C # Fundamentals

Ccf-csp 202109-3 pulse neural network, sometimes 100 points..

Leetcode 560. And is the prefix and of the subarray of K

RTSP/Onvif协议视频平台EasyNVR对静态文件大小的优化
随机推荐
独家 | 维信金科申请消费金融牌照未果
Ccf-csp 201403-3 command line options
postgresql判断数据库的主从关系
Collection tool class collections guide, and detailed explanation of comparable and comparator sorting
Ccf-csp 202104-1 gray histogram 100 points
Leetcode 1248. Count "graceful subarray" prefix and
Discussion on MLIR Technology
贪心法/哈夫曼编码
Basic method for missing data filling (2) -- random forest (missforest) filling
Qt: get sender in slot (the control that triggers signal)
Copywriting template used by 90% of we media professionals
Dest0g3 520 orientation web easyssti
Greedy method / non 01 knapsack problem
Ccf-csp 202203-3 computing resource scheduler 80 points
【HomeAssistant外网访问(cpolar)】
How does the rtsp/onvif protocol video platform easynvr configure the playback duration of a user's video stream?
Jerry: if the user doesn't need to use all the keys, how should other keys be set? [chapter]
Blue Bridge Cup_ Frog date_ Extended Euclid
Jericho's notes on SPI host configuration parameters: 【 chapter 】
Implementation of hash table of Telephone Query System for C language project (project requirements + operation interface + code analysis + complete code)