Answer the question
In order to leave comments, you need to log in
Convenient trading in Javascript?
Greetings.
In fact, it's not really about trading. Yes, there seems to be no “pure” trading in Javascript, but there are also webworkers, I know this, but here I want to raise a question about something else. To begin with, I will clarify what exactly I will mean by “trading”.
A typical task is the following. There is some code (for definiteness, wrapped in the myFunc function) and I want to run this code with a certain call, but without waiting for the completion of this call, go to the next line of code, from where I make this call. Usually this problem is solved like this:
setTimeout( myFunc, 10 );
doSomethingElse();
(function() {
var threads = [];
var messageName = "start-thread";
function thread(fn) {
threads.push(fn);
window.postMessage(messageName, "*");
}
function startThread(event) {
if (event.source == window &&
event.data == messageName) {
event.stopPropagation();
if (threads.length> 0) {
( threads.shift() )();
}
}
}
window.addEventListener("message", startThread, true);
Function.prototype.thread = function() {
var args = arguments;
var me = this;
thread(
function() {
me.apply( null, args );
}
);
}
})();
var doSomething = function(a, b) {
alert( a + b );
}
// вызываем doSomething() асинхронно:
doSomething.thread( 2, 3 );
myObject.myMethod.thread( arg1, arg2 );
myObject.myMethod.apply( myObject, [ arg1, arg2 ] );
Answer the question
In order to leave comments, you need to log in
mootools.net/docs/core/Native/Function - Function Method: pass
You can make a wrapper for closures so as not to spoil the code.
Just implement it in the form
myObject.myMethod.thread( arg1, arg2 );
unlikely to succeedmyObject.thread( 'myMethod', arg1, arg2 );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question