This question already has answers here:
async loading javascript with document.write
(5 answers)
How do I add a delay in a JavaScript loop?
(32 answers)
Closed 3 years ago.
I have a code to print a pyramid pattern using callback in javascript.
Right now I am able to load entire code after 2 sec.
I need to print pattern rows after 2 sec interval of time instead loading it at a whole using call back and promises . How can I do it ?
var n = 5;
for (var i = 0; i < n; i++) {
getStar(i, n).then(star => console.log(star));
}
function getStar(i, n) {
return new Promise((resolve, reject) => {
var str = '';
for (var j = 1; j < n - i; j++) {
str = str + ' ';
}
for (var k = 1; k <= (2 * i + 1); k++) {
str = str + '*';
}
setTimeout(() => {
resolve(str);
}, 2000)
})
}
Related
This question already has answers here:
Sum all the digits of a number Javascript
(8 answers)
Closed 1 year ago.
I tried to solve but didn't work :
const SumOf = (N) => {
var res = N.toString().split("");
var total = 0;
for (i = 0; i < N; i++) {
total += res[i]
}
}
You can simply write:
const sumN = (number) => {
const nArray = number.split("").map(n=> +n)
return nArray.reduce((acc, cur)=> acc+=cur, 0)
}
console.log(sumN("123"))
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
I am a bit new in JavaScript, trying to do the hardcoded part above in a more scalable way with the function below. The 't' + i functions well but do the same with the .t + i
function showInfo(results) {
var data = results.data
document.getElementById('t1').innerHTML = [data[0].t1].join(', ')
document.getElementById('t2').innerHTML = [data[0].t2].join(', ')
document.getElementById('t3').innerHTML = [data[0].t3].join(', ')
document.getElementById('t4').innerHTML = [data[0].t4].join(', ')
}
function showInfo(results) {
var data = results.data
for (let i = 1; i < data.length; i++) {
document.getElementById('t' + i).innerHTML = [data[0].t + i].join(', ')
}
}
do instead:
function showInfo(results) {
var data = results.data
for (let i = 1; i < data.length; i++) {
document.getElementById('t' + i).innerHTML = [data[0]['t' + i]].join(', ')
}
}
This question already has answers here:
Executing 100K Promises in Javascript in small 50 Chunks
(3 answers)
Closed 2 years ago.
I have a set of requests but they can not be called all simultaneously so I decided to split the set into the chucks of 10.
I am wondering how can I make 10 requests and wait for them all to complete like the example above:
data = []
for(mindex = 0; mindex < 1000; mindex = mindex + 10){
request_chunk = []
for(index = mindex+1; index < mindex+10; index++){
request_chunk.push(api.call(requests[index]).getPromise();
}
data = data + waitPromiseToComplete(request_chunk);
}
You can use Promise.all and await:
(async function () {
const data = []; // always declare variables!
for(let mindex = 0; mindex < 1000; mindex = mindex + 10){
const request_chunk = []
for(let index = mindex + 1; index < mindex + 10; index++){
request_chunk.push(api.call(requests[index]).getPromise();
}
data = data.concat(await Promise.all(request_chunk));
}
})();
This question already has answers here:
How do I add a delay in a JavaScript loop?
(32 answers)
Closed 7 years ago.
This creates a 2 second delay and runs the loop. I need to create a 2 second delay for every iteration, not just once.
var myArray = ['test1','test2','test3'];
function doSetTimeout(index) {
setTimeout(function() { console.log(myArray[index]) }, 2000);
}
var index;
for (index = 0; index < myArray.length; ++index) {
doSetTimeout(index)
}
Expected result would be:
test1
(2 second delay)
test2
(2 second delay)
test3
Just multiply your delay by the index
var myArray = ['test1','test2','test3'];
function doSetTimeout(index) {
setTimeout(function() { console.log(myArray[index]) }, index * 2000;
}
var index;
for (index = 0; index < myArray.length; ++index) {
doSetTimeout(index)
}
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 8 years ago.
What's wrong with this string: items[i].addClass('in'); ? it's killing me! I was tring different ways with .each(); for example but it's still fail...
if(layer = 'aboutAuthor') {
var items = $('.aboutAuthor .item');
var K = 100; // Коэфициент.
var t = K * (items.length + 1);
for (var i = items.length - 1; i >= 0; i--) {
console.log(items[i]);
setTimeout(function(){
items[i].addClass('in');
}, t);
t += K;
}
}
strange moment that console.log() displays each element in logs as expected, and normal. HELP!
I tried to not change the code too much, but I had assumed that you wanted to change the items in reverse.
if(layer === 'aboutAuthor') {
var items = $('.aboutAuthor .item');
var K = 100; // Коэфициент.
var t = K * (items.length + 1);
var i = items.length - 1;
$(items.get().reverse()).each(function() {
var item = $(this);
console.log(item);
setTimeout(function(){
item.addClass('in');
}, t);
t += K;
}
});
}