This question already has answers here:
Javascript Function setInterval() works only one time [duplicate]
(2 answers)
JS setInterval executes only once
(2 answers)
Why does the setInterval callback execute only once?
(2 answers)
Closed 6 months ago.
I am trying to make a chatbot for the web game https://skribbl.io but when my javascript script is run, it only runs once.
function send() {
var ic = document.getElementById("inputChat");
ic.value = "ChatBot online";
ic.parentNode.dispatchEvent(new Event('submit', {
bubbles: false,
cancelable: false,
}));
}
setInterval(send(), 1000);
setInterval(send, 1000);
or
setInterval(() => { send() }, 1000);
Related
This question already has answers here:
Is onload equal to readyState==4 in XMLHttpRequest?
(4 answers)
Why do we write onload() function before we write send() in XMLHttpRequest
(1 answer)
Closed 1 year ago.
Unable to get vanilla JavaScript syntax or maybe I have brainfreeze.
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
console.log('hello');
}
xhttp.open("GET", "my data.txt");
xhttp.send();
The onload function is defined and called immediately, why don't we need to call it seperately after defining like other normal functions?
This question already has answers here:
Calling functions with setTimeout()
(6 answers)
Closed 3 years ago.
I am trying to simulate a script that takes a long time to run on page load.
I tried this:
window.onload = function() {
setTimeout(alert("Page Rendered"), 200000);
};
But alert message happens instantly.
What am I doing wrong?
Check the function(). docs
window.onload = function() {
setTimeout(function(){alert("Page Rendered")}, 200000);
};
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I want to store the length of a Json array as a global variable to make my code more modular. I am trying to set the global variable inside the .onload function but it won't allow this. I have tried with a globalTest variable.
var objectLength = 0;
var globalTest = 0;
$(document).ready(function() {
establishConnection();
});
function establishConnection() {
xttp = new XMLHttpRequest();
xttp.open("GET", "http://exampleServerPath", true);
xttp.send("null");
xttp.onload = function() {
var Json = JSON.parse(this.response);
objectLength = Json.length;
globalTest = 2; // this doesn't work
};
globalTest = 4; //this works
}
I am fairly new to JS any help is appreciated!
I think the issue lies with the xttp.onload part. Change that to xttp.onreadystatechange and then check the readystate.
Check out this example.
EDIT:
Your code works as expected, but maybe you think globalTest is not being updated.
If you were to call establishConnection() and then immediately try to access globalTest it will still be 0 because the AJAX request has not completed yet.
If you did
establishConnection();
setTimeout(function() {
alert(globalTest);
}, 2000);
Then you should see the value you expect (assuming your ajax request completes in less than 2 seconds.
This question already has answers here:
Open a URL in a new tab (and not a new window)
(33 answers)
Closed 7 years ago.
I tried several things, but everytime Chrome (and others?) detect a pop up... is possible to bypass it ?
window.open(
'/test.php',
'_blank'
);
}, 2000);
You would have to open the window using javascript, then set a setTimeout for two seconds that would wait for a variable to be set.
The new window would have to set a variable in the parent window to, lets say true.
Then with the setTimeout runs, it checks whether the variable is true, and if not, then opens the link. If it is, then do nothing
var didItOpen = false;
window.open('page.html');
setTimeout(function () { if (!didItOpen) location.href = 'page.html'; }, 2000);
Can you please try the code below?
//1000 = 1 second
setTimeout(function () {
window.open('url here', '_blank);
}, 1000);
This question already has answers here:
How to return value from an asynchronous callback function? [duplicate]
(3 answers)
How do I return the response from an asynchronous call?
(41 answers)
Return Value from inside of $.ajax() function
(3 answers)
Closed 8 years ago.
I am running following javascript as a test using jsfiddle.net -
var v1=1;
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
alert('Your current position is:');
alert('Latitude : ' + crd.latitude);
alert('Longitude: ' + crd.longitude);
alert('More or less ' + crd.accuracy + ' meters.');
v1=crd.latitude;
alert(v1);
};
function error(err) {
alert('ERROR(' + err.code + '): ' + err.message);
};
alert(v1);
navigator.geolocation.getCurrentPosition(success, error, options);
alert(v1); //removing this makes the code work
The code works fine until the last alert is put in place. The success function isn't invoked in that case. I am baffled if it's a global variable declaration issue or the way getCurrentPosition is being invoked. All I want is to have the longitude and latitude values in a variable that I can use later. Newbie here. Any pointers?
Which Browser are you using? The above code works fine in chrome.
The second alert could be halting execution of your success function. better use console.log() for debugging.