How to put a text-to-speech in a loop [duplicate] - javascript

This question already has answers here:
JavaScript variable binding and loop
(4 answers)
Closed 4 years ago.
I'm using the js library p5.speech. I'm trying to get each strings throught the speech function. However, each time it repeats the same strings.
So is the issue coming from my code or the js library (and in this case, what library should i use?)
const btReport1 = document.getElementById('report1');
btReport1.addEventListener('click', function(e) {
myVoice.setVoice('Google UK English Female');
for(var i=0; i<allData.length;i++){
console.log("allData"+i+" = " + allData[i].profile.length);
// myVoice.speak(allData[i].profile.length+" patients have "+ allData[i].food);;
setTimeout(function afterTwoSeconds() { myVoice.speak(allData[i].profile.length+" dogs have "+ allData[i].food);}, 1000);
}
});

In your for loop, try replacing var with let so that
for(var i=0; i<allData.length;i++){
becomes
for(let i=0; i<allData.length;i++){
For more info, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

Related

JS: Using changing String in command after "." [duplicate]

This question already has answers here:
Reference a javascript property with a dynamic name
(3 answers)
Closed 3 years ago.
Currently I'm trying out some html and js stuff.
I have trouble using a changing string inside a command with some "." in it.
for (let i = 1; i <= 2; i++) {
let count = String("q" + i);
console.log(document.calculate.$(this.count).value);
}
So the String "count" is changing and I want the different values of my document with the names "q1", "q2"...
Would be awesome if someone could help me.
Thank You!
put dinamic value in a [] quotes instead .
console.log(document.calculate[$(this.count)].value);
for (let i = 1; i<=2; i++) {
let count = String("q" + i);
console.log(document.calculate.$(this[count]).value);
}

Javascript - update all divs of the same class in a loop [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
How do I add a delay in a JavaScript loop?
(32 answers)
Closed 5 years ago.
I am trying to update CSS for all divs (one by one) with a certain class using plain Javascript loop. Here it is:
var timerCoinRow = 0;
var rows = document.querySelectorAll('.coin-row');
for (var i=0; i<rows.length; i++)
{
timerCoinRow += 500;
obj = rows[i];
setTimeout(function ()
{
obj.style.opacity = 1;
}, timerCoinRow);
}
for some reason it does not work. here is JQuery version that works fine but I need it in plain "vanilla" Javascript:
var timerCoinRow = 0;
$('.coin-row').each(function ()
{
timerCoinRow += 500);
var obj =$(this);
setTimeout(function ()
{
obj.css('opacity', '1');
},timerCoinRow,obj);
});
When I place obj.style.opacity = 1; outside the timeout - all divs are updated but simultaneously while I need them to appear one after another with a pause of 500ms. Please help - it's somewhere near but ....

How to create functions with dynamic content and store them in an object? [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 6 years ago.
I want to create multiple functions that do different things depending on what's written in a data-table and store them in an object for later use.
Let's look at this simple example:
var arrayPl = [];
function myFunction() {
var carNames = ["Volvo", "Nissan"];
var counter = 0;
for (var i in carNames) {
arrayPl[counter] = function() {
alert(carNames[i]);
};
counter++;
}
}
myFunction();
arrayPl[0]();
Here I wanted to create as many functions as there are car names and save them in an object arrayPl that I could call these functions from later.
Now obviously this example doesn't explain why I would need to do this - but at least it shows my problem, because the first function arrayPl[0](); gives me the second car name instead of the first.
Explaining the reason behind this is too complicated for me now and not that important (Dialogs in Adobe LiveCycle) - I just want to know if what I'm asking is possible in general and if so how.
I prefere this syntax :
var arrayPl = [];
function myFunction() {
var carNames = ["Volvo", "Nissan"];
carNames.forEach( function(element, i) {
arrayPl[i] = function() {
alert(carNames[i]);
}
})
}
myFunction();
arrayPl[0]();

Assigning a value to an object children with a for loop doesn't work [duplicate]

This question already has answers here:
jQuery AJAX calls in for loop [duplicate]
(2 answers)
Closed 6 years ago.
http://codepen.io/noczesc/pen/ZWppJQ?
function codeToName(data) {
for (var i = 0; i < data.country.borders.length; i++) {
$.getJSON("https://restcountries.eu/rest/v1/alpha?codes=" + data.country.borders[i], function(json) {
data.country.borders[i] = json[0].name;
console.log(json[0].name);
});
}
};
I'm getting an array of country codes which are supposed to be changed to their full representations in English via the codeToName loop and an API, but it only appends a random name to the end of the array. To get a console.log of it, click on the <body>. It's located in JSONextract.country.borders. The country names grabbed via API are correct (logged in console also), but they don't get assigned to the variables inside my object. How can I solve this issue?
for does not work, you should use $.each
function codeToName(data) {
$.each(data.country.borders, function(index,item){
$.getJSON("https://restcountries.eu/rest/v1/alpha?codes=" + item, function(json) {
data.country.borders[index] = json[0].name;
console.log(json[0].name);
});
});
};

How to loop function in javascript [duplicate]

This question already has answers here:
Javascript infamous Loop issue? [duplicate]
(5 answers)
Closed 8 years ago.
I have same function that I want to make it based on user input ,
for(var u = 1; u <= tmp; u++){
$('#photo_'+u).change(function(){
$('#src_'+u).val($(this).val());
});
Caption: tmp is user input
I try jshint in jsfiddle showing error : "Don't make function withit loop"
how to make that function loop
My question is based in this Link Here
change id and use class:
$('.photo').change(function(){
$(this).find('.src').val($(this).val());
});
change #photo_... and #src_... to class.
$(this) will to point to current .photo element
no need for a loop in this case...
var functionStr="";
for(var u=1;u<=tmp;u++){
if(u==tmp)
{
functionStr=functionStr+"#photo_"+u;
}
else
{
functionStr=functionStr+"#photo_"+u+",";
}
$(functionStr).change(function(){
$('#src_'+u).val($(this).val());
});

Categories