How to make the event handler non-blocking in Javascript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Here, If you typed a key in the input and look at the browser console, you can see that the typed letter will appear here after the execution of the for-loop. How can I make the letter appeared instantly?
document.querySelector('input').addEventListener('input', () => {
for(let i=0; i<=1000; i++) {
console.log(i);
}
})
<input type="text"/>

Can you try?
document.querySelector('input').addEventListener('input', async () => {
for(let i=0; i<=1000; i++) {
console.log(i);
}
})
Apparently your callback is blocking execution of thread, that would be the reason that it has to wait until for loop is completed.

Related

How can I detect if user has successfully alerted a thing (I'm making an XSS game) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The page just echoes the user input using GET. I have no idea on what concept to apply to detect if the user has alerted something
The easiest approach would probably be to replace the alert method with your own.
const log = [];
alert = function(alert) {
return (value) => {
log.push(value);
alert(value);
};
}(alert);
alert("Hello!");
console.log(log);

Loop with setTimeout with different delays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have to make a kind of playlist of events, where each event has a specific play time and the next one has to play after the time's up and so on.
Currently I'm using the setTimeout function. Now I would need to loop to get my events and times.
My code:
for(let i = 0; i < playlist.length ; i++){
setTimeout(function(){
$('#ventana').attr({src: playlist[i].url_play});
}, playlist[i].time_play*i);
}
But the timing is not right. How can I correct this?
It will indeed not work if you set the delay to playlist[i].time_play*i: think of it... It does not take into account the delays for past playlists, only the current one -- multiplied by its index(??).
Instead you would need to accumulate the different delays, for instance in a delay variable:
for (let i = 0, delay = 0; i < playlist.length ; delay += playlist[i].time_play, i++) {
setTimeout(function() {
$('#ventana').attr({src: playlist[i].url_play});
}, delay);
}

Why JavaScript Alert Message is not working ? Code Error cannot be indicated [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why JavaScript Alert Message is not working ?
var ckc = prompt ("Enter Your City");
ckc = ckc.toLowerCase();
var cities =["Aaan", "Baan", "Caan" ,"Daan"];
for (var i=0; i<=4 ; i++){
if (ckc === cities[i]){
alert ("Hum, Nice City");
}
}
Use:
if (ckc === cities[i].toLowerCase()){
since you have downcased ckc and cities array has upcase values, your original if condition would never work.
Remove this code
ckc = ckc.toLowerCase();

My first permutation lesson - what is this code missing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Translated (apparently wrongly) from a C++ book.
If I can get it to work, then I can start trying to understand it.
function recPermute(soFar, rest)
{
if (rest==="")
{
console.log(soFar);
}
else
{
for(i=0; i<rest.length; i++) // <<< error was in not declaring the "i"
{
var next = soFar + rest[i];
var remaining = rest.substr(0,i) + rest.substr(i+1);
recPermute(next, remaining);
}
}
}
function listPerm(s)
{
recPermute("",s);
}
listPerm("kitcap")
You need to declare i so it's scoped to recPermute:
for(var i=0; i<rest.length; i++)
Without the var, it'll be created as a global so each call to recPermute will alter it for any other calls.
for JavaScript, use charAt(), instead of using array like acessing.
var next = soFar + rest.charAt(i);
One thing that could be an issue is you are using effectively the same i for each call to the function. You need to declare a local i or it will be declared in the global scope.
for(var i = 0; ....

Create buttons dynamically in a div from a javascript function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a function that returns the data from the sql...
I need to create buttons in a div called
divResult
here is my function
function retrieveData(transaction, results) {
for(var i = 0; i < results.rows.length; i++) {
var data = results.rows.item(i)['nome'];
alert(data);
}
}
There is a very good answer on how to create html elements using javascript here: Creating Dynamic button with click event in javascript.
...I would add this as a simple comment but I don't have enough street cred yet.

Categories

Resources