closures

Javascript Closure Problem

≯℡__Kan透↙ 提交于 2019-12-02 12:22:06
I know this kind of question gets asked alot, but I still haven't been able to find a way to make this work correctly. The code: function doStuff () { for (var i = 0; i< elementsList.length; i++) { elementsList[i].previousSibling.lastChild.addEventListener("click", function(){ toggle(elementsList[i])}, false); } } // ends function function toggle (element) { alert (element); } The problem is in passing variables to the toggle function. It works with the this keyword (but that sends a reference to the clicked item, which in this case is useless), but not with elementsList[i] which alerts as

Convert anonymous function in PHP 5.3 into PHP 5.2 equivalent

家住魔仙堡 提交于 2019-12-02 12:14:32
问题 I have error in line 2 and 13 in PHP 5.2, I have no idea to make the correction, I tried using create_function but not working, can anyone help with this? function _process_special_keyword($str){ $callback = function($match){ $ret = $match[1] . '[' . $match[2] . ']'; if(!empty($match[3])){ $ret .= '.[' . $match[3] . ']'; } $ret .= $match[4]; return $ret; }; $strSQL = preg_replace_callback('/([\s\(\.,])(' . SPECIAL_KEYWORDS . ')(?:\.(' . SPECIAL_KEYWORDS . '))?([\s\)\.,])/i', $callback, $str);

JavaScript closure loop issue in set time out

陌路散爱 提交于 2019-12-02 11:56:27
问题 I've found some example in a tutorial (said it was the canonical example) for (var i=1; i<=5 ; i++) { setTimeout(function() { console.log("i: " + i); }, i*1000); } Now, I understand that, closure passes in the current scope in to the function, and I assume that it should output 1,2,3,4,5. But instead, it prints number 6 five times. I ran it in the chrome debugger, and first it goes through the loop without going in to the function while doing the increment of the i value and only after that,

Iterate and print content of groovy closures

﹥>﹥吖頭↗ 提交于 2019-12-02 11:37:42
问题 In a loop I create 4 closures and add them to a list: closureList = [] for (int i=0; i<4; i++) { def cl = { def A=i; } closureList.add(cl) } closureList.each() {print it.call()println "";}; This results in the following output: 4 4 4 4 But I would have expected 0,1,2,3 instead. Why does the 4 closures have the same value for A? 回答1: Yeah, this catches people out, the free variable i is getting bound to the last value in the for loop, not the value at the time the closure was created. You can

Broken closure - please help me fix it

主宰稳场 提交于 2019-12-02 10:58:09
in a related question I have posted this code. It almost works, but the counter doesn't. Can we fix it? (no jQuery, please) <script type="text/javascript"> var intervals = []; var counters = { "shoes":{"id":"shoe1","minutes":1,"seconds":5}, "trousers":{"id":"trouser1","minutes":10,"seconds":0} }; // generate this on the server and note there is no comma after the last item window.onload = function() { for (var el in counters) { countdown(counters[el]) }; } function countdown(element) { intervals[element.id] = setInterval(function() { var el = document.getElementById(element.id); var minutes =

JavaScript Event implementation to Closure based Object

牧云@^-^@ 提交于 2019-12-02 10:08:25
I have a Object based on some closure, and want to implement event scheme here: var class1 = function(val1) { var val = val1; //------ want to call a method of Object of class1-------- var self = this; setTimeout(function() { self.onEvent(); }, 1000); //---------------- return { f1: function() { return val; }, onEvent: function() { console.log('not implemented yet. Override'); } }; }; var obj1 = class1(5); console.log(obj1.f1()); //5 obj1.onEvent(); //not implemented yet. Override obj1.onEvent = function() { console.log('event fired'); } got error, and I know the reason, and I need a solution:

What are the correct semantics of a closure over a loop variable? [closed]

走远了吗. 提交于 2019-12-02 09:25:28
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Consider the following lua code: f = {} for i = 1, 10 do f[i] = function() print(i .. " ") end end for k = 1, 10 do f[k]() end This

multithreading re-entrancy issue

那年仲夏 提交于 2019-12-02 09:17:36
I'm trying to spawn different threads for some processing. I use the for loop index for some logic inside each thread. How can I get the different threads to print 1,2,3,4, 5 in the code below? Each time I run this, I get different numbers as output - 3,3,3,4,6,6 & 2,2,3,5,5,6 etc. I tried using the lock object, but it stil wasn't doing it correctly. Can anyone help me achive this. I just want to make sure each thread/task gets the right index. Note that each task has been forced to run on a separate thread. List<Task> tasks1 = new List<Task>(); for (int j = 1; j <= 5; j++) { tasks1.Add(Task

How to use closures to create event listeners in a Javascript for loop?

▼魔方 西西 提交于 2019-12-02 08:54:41
问题 HTML <span class="char" id="0">?</span> <span class="char" id="1">!</span> <span class="char" id="2">"</span> <span class="char" id="3">/</span> <span class="char" id="4">%</span> <span class="char" id="5">$</span> ... JavaScript var charElems = document.getElementsByClassName('char'); for (var i=0; i < charElems.length; i++) { charElems[i].addEventListener('mouseover',function() { (function(j) {mouseoverCheck(j);}(i)); }); } I have a bunch (hundreds) of span elements with numbers as IDs

Array of promises [duplicate]

余生颓废 提交于 2019-12-02 08:32:32
This question already has an answer here: Promise.all().then() resolve? 2 answers I am working on a piece where I have to get json data from an API for different cities and build DOM. So far, I have been able to do both. Only problem is that the API response time for the different cities are different. So, when I build the DOM they are not in the same order as I call the function. From what I recall I need to use promise to get it that order. My questions now are: How can I use an array of promises (since my input will vary). Also how do I execute an array of promises? My working code so far: