Jquery/Ajax call with timer
If you want to set something on a timer, you can use JavaScript's setTimeout
or setInterval
methods:
setTimeout ( expression, timeout );
setInterval ( expression, interval );
Where expression
is a function and timeout
and interval
are integers in milliseconds. setTimeout
runs the timer once and runs the expression
once whereas setInterval will run the expression
every time the interval
passes.
So in your case it would work something like this:
setInterval(function() {
//call $.ajax here
}, 5000); //5 seconds
As far as the Ajax goes, see jQuery's ajax()
method. If you run an interval, there is nothing stopping you from calling the same ajax()
from other places in your code.
If what you want is for an interval to run every 30 seconds until a user initiates a form submission...and then create a new interval after that, that is also possible:
setInterval()
returns an integer which is the ID of the interval.
var id = setInterval(function() {
//call $.ajax here
}, 30000); // 30 seconds
If you store that ID in a variable, you can then call clearInterval(id)
which will stop the progression.
Then you can reinstantiate the setInterval()
call after you've completed your ajax form submission.
Repeat/loop ajax call on a timer - even if timed out
I'd prefer a solution that only queued a new call when the current had completed. something like..
function poll() {
setTimeout(function () {
GetData();
}, 1000);
}
function GetData() {
jQuery.ajax({
url: "something.ashx",
type: "GET",
contentType: 'application/json; charset=utf-8',
success: function(resultData) {
//...
},
error : function(xhr, textStatus, errorThrown) {
//...
},
complete: function() {
poll();
},
timeout: 0,
});
}
poll();
This way your calls will not risk overlapping anyway.
jQuery/AJAX call with a timer
When the ajax call returns from the server, create a timer that will poll the server again n
milliseconds later. This approach should work regardless of how long it takes for the ajax call to complete.
$("button").click(function refreshText(){
$.ajax({
url:"http://127.0.0.1:8080/",
success:function(result){
$("#text").val(result);
setTimeout(refreshText, 5000);
}
});
});
Call jQuery Ajax Request Each X Minutes
You can use the built-in javascript setInterval.
var ajax_call = function() {
//your jQuery ajax code
};
var interval = 1000 * 60 * X; // where X is your every X minutes
setInterval(ajax_call, interval);
or if you are the more terse type ...
setInterval(function() {
//your jQuery ajax code
}, 1000 * 60 * X); // where X is your every X minutes
Ajax call and countdown timer
Something like this should work fine. Start an initial timeout, then set a timeout whenever you get a response.
var requeue = function() {
setTimeout(performRequest, 1000);
};
var performRequest = function() {
var request = $.ajax({
url: "/rhino",
type: "GET",
dataType: "json"
});
request.done(function (data) {
console.log("success", data);
requeue();
});
request.error(function (data) {
console.log("failed");
requeue();
});
};
requeue();
working plunker
Ajax call on setInterval timer not working
Found the answer here:
jQuery ajax only works in IE when the IE debugger is open
Turns out iE, and only IE, will cache ajax responses. You have to tell it not to. Adding cache: false
did the trick.
function GetData(isFirstLoad) {
//console.log("Attempting to obtain the data...");
jQuery.ajax({
url: "something.ashx",
type: "GET",
contentType: 'application/json; charset=utf-8',
cache: false,
success: function(resultData) {
Jquery ajax post with timer event?
The first parameter to setInterval
should be a function (or an eval
able string). Right now, you are calling PostMainChartValues()
and passing its return value to setInterval()
.
Change it to:
window.setInterval(function() {
PostMainChartValues(meter_id, range_id);
}, 5000);
Ajax call doesn't show correct timer time iPad
I found the soluition myself yesterday in the evening
This is what it looked like before
if (cooking) {
let timeElapsed = new Date("2019-11-07 10:02:15").getTime();
run(timeElapsed);
}
But I found out that an iPad (or maybe IOS in general) doesn't know how to handle that datetime format with the new Date([DATETIME]). So I had to change the '-' with an '/'.
So now it looks like this:
if (cooking) {
let timeElapsed = new Date("2019-11-07 10:02:15".replace(/-/g, '/')).getTime();
run(timeElapsed);
}
This is the soluition
Ajax with jquery to set time interval
If you just want to call it every 10 seconds, use 10000
milliseconds in the setTimeOut
. Also, it is best to call again the function only when the previous Ajax call is done:
$(document).ready(function(){
$(function(){
var test = "";
function inTime(){
$.POST("check_new_reply.php",{testing:test}, function(data){
$("#message-u").html(data);
setTimeout(inTime, 10000);
});
}
inTime();
});
});
Related Topics
Origin <Origin> Is Not Allowed by Access-Control-Allow-Origin
Reactjs Async Rendering of Components
Does JavaScript Support 64-Bit Integers
What Is the Most Efficient Way to Concatenate N Arrays
Create a Simple 10 Second Countdown
Howto: Div with Onclick Inside Another Div with Onclick JavaScript
How to Change Only Text Node in Element
Using Canvas to Animate a Sorting Algorithm in Js
How to Open Specific Tab of Bootstrap Nav Tabs on Click of a Particuler Link Using Jquery
Ternary Operators in JavaScript Without an "Else"
Track Event in Google Analytics Upon Clicking Form Submit
What the Difference of This.State and This.Setstate in Reactjs
What Is "Function*" in JavaScript
How to Display Pie Chart Data Values of Each Slice in Chart.Js
Closing Websocket Correctly (Html5, JavaScript)