Jquery ajax, $.each wait to finish - ajax

I've been looking for the last couples days for a good solution for this problem. but can't seem to find one.
I have a Jquery script that has to loop through each element get a value and then do an Ajax call, however this is thousands of elements so i would like for the script to go element by element and only move to the next one once the previous action has finished, i tried asyc but that does not seem to wait properly and causes the page to freeze and miss a lot of elements and cause errors.
what advise/script would you suggest i try or look into?

I found a solution to my problem, but it's probably very specific, since i have unique ID's for each element what i did was:
first i defined an empty array and a variable for counting
on the initial click for each loop placing each SKU in an array, once done execute the ajax function
ajax function get's the index of current count then executes function
then i +1 the count and if it is less than the length of the array call ajax function again
i tried to explain it as open as possible in case someone has the same sort of situation, but you can always find a way to assign a random value to your element to somehow identify it

Related

Assigning proper value from for loop in GUIs

I have been learning the Fyne library for making GUIs in Go, and have run into a problem.
func createResponses(content *fyne.Container){
for i:=0;i<address.childrenCount;i++{
content.Add(widget.NewButton(address.children[i].text,func(){
fmt.Println(i)
}))
}}
This is supposed to iterate through the children of a node, add a button with the text stored in that node, and then when clicked, print the value of i when it was created to the console. However, while the text of the button, the first parameter (address.children[i].text), displays correctly, the value of i always prints out the same, as 3, the number that causes the loop to terminate.
At first I though this was a specific problem to Fyne, or my poor understanding of how to use Go in general, but I recall having a similar problem when I first learned JavaFX. Is there something about GUIs that causes this behavior to emerge? More importantly, what is the proper way of dealing with this problem? Thank you!
This is to do with using a delayed function inside the for loop. By the time the code executed the i variable will be at the last value. Solution is to “capture” the value before passing it to the callback.
for i:=0;i<address.childrenCount;i++{
index := i
content.Add(widget.NewButton(address.children[i].text,func(){
fmt.Println(index)
}))
}

How to setup the data structure?

First let me explain what I want to do. I have n data elements. Every element needs to be checked with every other element but not with itself. The function that checks the elements returns true if everything is ok. If something is wrong than the function deletes both elements and replace them with new ones. But the new ones need to be checked with every other element too. This will be repeated until every element was checked with every other and all checks are fine.
I am asking how to setup the data structure in a most efficient way. When I test ele1 with all other n-elements and all are fine and then I test ele2 with ele84 and both get replaced I need to check ele1 and ele2 again, if these are now not fine I need to check all for ele1 again. But how to remember in the most efficient way which elements need to be check and which don’t to avoid double checking of elements?
You can use three lists: Main, CurrentNew, and NextNew
Main is initialized with the elements - use a nested loop to check them all, if you delete any elements then add the new elements to NextNew
On the next iteration, NextNew becomes CurrentNew - allocate a new NextNew list. First use a nested loop to check all elements of CurrentNew with the other elements of CurrentNew, then check the elements of Main with the elements of CurrentNew. New elements go to NextNew. Note that you're not checking the elements of Main with the other elements of Main - you already know that they're valid with each other.
On the next and each subsequent iteration, merge CurrentNew into Main, then NextNew becomes CurrentNew, repeat until all elements are valid.

App Inventor TinyWebDB List Problems

I got a problem using the TinyWebDB in App Inventor 2. Here's a Screenshot of the blockcode.
The goal of this Screen is to store a list(array) of images and later query them with a button but my problem starts already earlier. First there is a variable initialized called fotoList and declared as an empty list.
When this Screen initializes (left block) I store the empty fotoList under the tag FotoListTag. Then if the image under the tag "SteckbriefFoto" is not in this list -> getValue with tag "FotoListTag". Then he jumps into the block on the right and adds the photo .. other stuff not important .. at the end I store the list again in the TinyWebDB (and also in the TinyDB) with the tag "FotoListTag". Then it goes back to the block on the left where at the end I want to set an image.picture to the photo I stored in variable fotoList.
When I compile the code there is an error opening the page that says
Select list item: List index too large
Select list item: Attempt to get item number 1 of a list of length 0:()
I just don't get the problem with this code and i hope someone can help me.
For lists, valueIfTagNotThere should be create empty list instead of an empty string
On first run of your app, TinyDB is empty, which means, for tag = FotoListTag you get no value back, therefore this should be an empty list in the beginning.
Later you are trying to select the first item from the list (zahl is 1). As you know, the list is empty in the beginning, so probably you should add an if statement to check, if the list is not empty and only then select the first item... same for tag = Schriftlist.
You also have a timing issue. in Screen.Initialize you are trying to get a value from TinyWebDB. This is an asynchronous call, you get the result back in TinyWebDB.GotResult event and this takes a little bit (let's say 500 milliseconds), but meanwhile the complete blocks of the Screen.Initialize event will be executed. Probably you are expecting, that meanwhile tag = FotolistTag is not empty anymore, but this is not the case.

The "getnodevalue" of HTMLUNIT not working on domattr

every time i want to get the Value of my DomAttr i get an TypeError:
My Code:
Wanted = page.getByXPath("//span[contains(.,'Some')]/parent::a/#href");
return this
[DomAttr[name=href value=URLSTRING]]
Now i want to geht the value (=URLSTRING) with Wanted.getNodeName();
but every Time i get the Error
Cannot find function getNodeValue in object [DomAttr[name=href value=
same when i use getValue
please help me
There are some things that make no sense in the code (particularly, because it is not complete). However, I think I can guess what the issue is.
getByXPath is actually returning a List (funny thing you missed the part of the code in which you specify it as a list and replaced it with a Wanted).
Note you should probably also have type warnings in the code too.
Now, you can see that the returned value is in square brackets. That means it is a List (confirming first assumption).
Finally, although you happened to miss that part of the code too, I guess you are directly applying the getValue to the list instead of the DomAttr elements in the list.
How to solve it: If you need more than 1 result iterate over the elements of the list (that Wanted word over there). If you need 1 result then user the getFirstByXPath method.
Were my guesses right?

Prototype $$ returns array, should return one element like $

When using the dollar-dollar-function in prototype I alway get an array of elements back, instead of just one element with the dollar-function. How can I combine the power of CSS-selectors of $$ but still get only one element back?
Changing the structure of the source is not possible, so I can't just select it with the id. It needs to get selected with CSS, but should just return one element.
You can also do
$$('.foo').first()
It looks cleaner than $$('.foo')[0] for my taste :)
It does not make sense to return a single element when selecting by class name because potentially there could be many elements in the DOM that have this class. So you could always use the first element of the returned array if you are sure that it will be unique.
$$('.foo')[0]

Resources