closures

Javascript setTimeout in foreach: need help creating a closure

自作多情 提交于 2019-12-05 03:27:25
I have this function notes.forEach(function(note) { setTimeout(function() { playNote(note); }, 1000); }); This doesn't work. It plays all the notes at the same time, instead of playing them sequentially with a 1 second gap in between. It looks like I need to have a closure here to make this work. Could someone help me fix this function so it would play the note with the delay between each note? Cracker0dks because all timeouts are set at the same time... Do something like this: playAllNotes(0); function playAllNotes(index) { if(notes.length > index) { setTimeout(function() { playNote(notes

Command failed due to signal: Segmentation fault: 11 while emitting IR SIL function

只谈情不闲聊 提交于 2019-12-05 03:21:42
I want to add closure properties in the extension of UITextView so I define a closure using typealias: typealias TextViewHeightDidChangedClosure = (_ currentTextViewHeight:CGFloat)->Void extension UITextView{ func setTextViewHeightDidChanged(textViewHeightDidChanged:TextViewHeightDidChangedBlock){ objc_setAssociatedObject(self, &TextViewHeightDidChangedBlockKey, textViewHeightDidChanged, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC) } func textViewHeightDidChanged()->TextViewHeightDidChangedBlock?{ let textChanged : ((CGFloat)->Void) = objc_getAssociatedObject(self,

Return from closure?

不问归期 提交于 2019-12-05 02:40:50
问题 How does one return from a closure, without returning from the containing function? In the following function, the return statement actually returns from GM_xmlhttpRequest : not the closure. Naturally I can see that I could arrange my code so that that execution drops off the end of the closure, but I'm curious as to how to early return in the example. function GM_xmlhttpRequest(details, callback) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState != 4)

Javascript closures - variables vs parameters

笑着哭i 提交于 2019-12-05 02:29:26
I'm trying to learn Javascript closures. I'm having trouble getting my head around the fact that when you create several closures in a loop, all closures save only the last state of a variable. With this example var links = document.getElementsByTagName('a'); for (var x=0; x<links.length; x++) attachListener(); function attachListener() { links[x].addEventListener('click', function(){ console.log(x); }, false); }; When I have three links in my document, clicking on any link shows "3", I guess because x got incremented to 3 after the final run of the loop. I read in this excellent intro that if

How to copy a variable in JavaScript?

家住魔仙堡 提交于 2019-12-05 02:15:16
问题 I have this JavaScript code: for (var idx in data) { var row = $("<tr></tr>"); row.click(function() { alert(idx); }); table.append(row); } So I'm looking through an array, dynamically creating rows (the part where I create the cells is omitted as it's not important). Important is that I create a new function which encloses the idx variable. However, idx is only a reference, so at the end of the loop, all rows have the same function and all alert the same value. One way I solve this at the

Will java allow to use functional interfaces as methods?

爷,独闯天下 提交于 2019-12-05 02:06:05
With the new java lambdas and the concept of functional interfaces, will it be possible to treat those functional interfaces as methods? interface Func { void execute(int i); } void call(Func f) { f(1); //instead of f.execute(1); } I found a lot of information about the syntax of actual lambda expressions, but nothing about this. Your proposition What you propose has been discussed on the lambda-dev mailing list before: http://mail.openjdk.java.net/pipermail/lambda-dev/2012-February/004518.html It was mainly rejected because of the various problems related to ambiguity that would arise for the

Adding HTML to Drupal closure?

让人想犯罪 __ 提交于 2019-12-05 02:01:37
问题 To add javascript, you can use: drupal_add_js And similar for css: drupal_add_css But what if I just want to add html at the end of my page. I.e. Add a div with some text in it at the end of the page? 回答1: A lot of suggestions here work if you want your alteration to be theme-based, but if you want this to come from a module, using a block region, page template or page prepocess won't cut it, because you're tying the alteration to the theme. From a modular standpoint, the following options

How to scope closure's variables in CF10?

白昼怎懂夜的黑 提交于 2019-12-05 01:56:50
Quoted from the Adobe ColdFusion 10: Using closures documentation : function helloTranslator(String helloWord) { return function(String name) { return "#helloWord#, #name#"; }; } How to scope helloWord and name properly on the return line? Are they both in Arguments scope? If that's the case, they must be unique? The Closures and functions section also mentions there are 4 more scopes on top of the already long scope search: In closure, following is the order of search for an unscoped variable: Closure's local scope Closure's arguments scope Outer function's local scope if available Owner

Is it possible to create function-local closures pre-C++11?

烂漫一生 提交于 2019-12-05 01:45:29
问题 With C++11, we get lambdas, and the possibility to create functions/functors/closures on-the-fly where we actually need them, not somewhere where they don't really belong. In C++98/03, a nice way to make function-local functors/closures would've been the following: struct{ void operator()(int& item){ ++item; } }foo_functor; some_templated_func(some_args, foo_functor); Sadly, you can't use local types for templates (Visual Studio allows this with language extensions enabled). My train of

Making variables captured by a closure volatile

扶醉桌前 提交于 2019-12-05 01:26:46
How do variables captured by a closure interact with different threads? In the following example code I would want to declare totalEvents as volatile, but C# does not allow this. (Yes I know this is bad code, it's just an example) private void WaitFor10Events() { volatile int totalEvents = 0; // error CS0106: _someEventGenerator.SomeEvent += (s, e) => totalEvents++; while(totalEvents < 10) Thread.Sleep(100); } EDIT : People seem to be missing the point of my question a bit. I know I can't use volatile on local vars. I also know that the example code code is bad and could be implemented in