views:

69

answers:

3

Hi!

I'm just looking for an answer on the net but I haven't reached any one. I'm playing with google's api translation and I have a problem with JS. In fact my code is 100% ok except one operation.

My problem is that the translation is not given at the time. After js sends the information to google it doesn't wait until the translation is given. Instead of this it continues reading my code so it doesn't stop for get an answer. It proceess the information to be translated and then the answer will be get some time after. So because I want to do translations of translations I have done a sweet loop. Because I don't know how to stop js, I have done a page with diferent inputs text box and in my loop when the data is received it's send to the value of the text box and here is where it becomes the problem. I want to enter to my js function again when the text is changed so If js changes the value it will return to my loop when I get the translation. Simply and effective but if I put in the input box:

onchange="myfunction()"

the loop doesn't work. But the strangest is that if I manually change the text then the function works so it's as if onchange only works when a human change the text but not if js change the text. Is there any solution? May be some dinamic listener or something like that?

+1  A: 

The onchange event only fires if the user changes the value of the input. It isn't supposed to fire if the input is changed programmaticly.

Call the function from whatever function sets the value instead.

David Dorward
I CAN'T. I do not know when I will get the answer and if I call the function inside the bucle it will call the google's API before it get the first answer
Eureka
How is it setting the value of the input if it doesn't wait until the result comes back?
David Dorward
google.language.translate(text, "en", "fr", function(result) { document.getElementById('translation').value = result.translation;});
Eureka
Js does this line and then continues with the next lines without the answer. Then later when the answer is given magically this line to set the value works.
Eureka
Well yes. It's a callback, a common pattern in event driven programming (which most JS is). You pass a function as an argument. That function is called when the data comes back. **Do the work in that function**
David Dorward
Oh damm. Ok you're correct. I tried this before but different because the first time I do a translation inside this translation call and the results weren't good. Thank you very, very much. You got the answer to my question :)
Eureka
+1  A: 

The onchange event is designed to fire when the user (and only the user) changes the value and for that reason it is fired only after the value was changed AND field lost focus.

Ivan Ferić
Is there someone to be fired only after change the value but not lost focus?
Eureka
Not that I know of. Maybe you could focus on something other than onchanged event for text input. Does the function that changes your text input do anything else? For example, maybe it calls submit form - if so you could listen to onsubmit event of the form element.
Ivan Ferić
The problem is that if I write some code to change something more it will be done before the text is written in the text box. Sounds absurd but...
Eureka
+1  A: 

Helpful Starters and that Bucle you speak of


  1. You should look at the onkeyup or onblur.
  2. If the input value is being set by JavaScript, then why not call the event after setting it?

I have no idea what a bucle is.
My best guess: it is Spanish for 'loop'

vol7ron
Opsss, sorry my mistake. Yeah you're correct, it's loop.
Eureka