Transferring objects in the window.setTimeout method_javascript tips
The syntax is:
window.setTimeout(expr,msec)
expr is the execution string, and after msec milliseconds, it will be run as js. I just discovered yesterday that expr can also be a function. Haha, using this feature, objects can be transferred.
The following code implements the function of delayed transmission of object p in function foo1 to function foo2.
[Ctrl A to select all Note: If you need to introduce external Js, you need to refresh to execute
If you need to introduce external Js, you need to refresh To execute
][Ctrl A Select all Note:
If you need to introduce external Js needs to be refreshed to execute
]
Many frameworks now actually directly use a multi-cast event (its implementation principle is not complicated). A multi-cast event itself is actually a standard function. But it generally has the following methods.
MuEvent.add = function (func) { ... }
MuEvent.addMethod = function (instance, func) { ... }
When using the first method, when activating the func event processing function, use the current instance of MuEvent as this object; the second method uses the incoming instance as this object.
So for setTimeout, our traditional method is to use it to activate the method like this:
----------
function doTimer() {
obj1.call();
obj2.call();
}
setTimeout(doTimer, 1000);
and use multi-cast events The code can be as follows:
----------
var e = new MuEvent();
e.addMethod(obj1, obj1.call);
e.addMethod(obj2, obj2.call);
setTimeout(e, 1000);
----------
Of course , if you want to write more COOL, you can do it like this:
----------
setTimeout(function() {
return new MuEvent (obj1, obj1.call, obj2, obj2.call);
}(), 1000); <script> foo1() function foo1(){ var p={x:3,y:4} window.setTimeout(function(){foo2(p)},100) } function foo2(p){ alert(p.x) } </script>---------- <script> foo1() function foo1(){ var p={x:3,y:4} window.setTimeout(function(){foo2(p)},100) } function foo2(p){ alert(p.y) //输出foo1的p={x:3,y:4} 的y } </script><script> var pp=3; foo1() function foo1(){ var p={x:3,y:4} var pp=1; window.setTimeout(function(){foo2('pp')},100) window.setTimeout("(function(){foo2("+pp+")})()",200) //也就是说,这个函数其实将参数值传过去.相当于如下将foo2引用进来运行: window.setTimeout("(function(){alert("+pp+")})()",200) } function foo2(pp){ alert(pp) var pp=2; alert(pp) } </script>As a little introduction , the Qomo I made is MuEvent implemented in this form. Most frameworks like Atlas adopt a similar approach.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

This article explores effective use of Java's Collections Framework. It emphasizes choosing appropriate collections (List, Set, Map, Queue) based on data structure, performance needs, and thread safety. Optimizing collection usage through efficient

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.
