score:-1
If you use jquery, then you can send an AJAX request using the $.ajax function
. Make sure you handle the response in the result's done()
function
, as success
is deprecated.
Plain AJAX request example:
HTML:
<!DOCTYPE html>
<html>
<body>
<div id="demo"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadDoc()">Change Content</button>
</body>
</html>
JS:
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
Taken from here. If you mastered AJAX requests, then the next step is to write a poller, using setInterval. The first parameter should be a function
which sends a request and the second should be the time between two execution in milliseconds (10000 in this case). Or you can use an existing poller. This is one I have implemented:
function Initializable(params) {
this.initialize = function(key, def, private) {
if (def !== undefined) {
(!!private ? params : this)[key] = (params[key] !== undefined) ? params[key] : def;
}
};
}
function Poller(params) {
Initializable.call(this, params);
var that = this;
this.initialize("url", window.location.href);
this.initialize("interval", 5000);
this.initialize("type", "POST");
this.initialize("method", "POST");
this.initialize("data", {});
this.initialize("strict", true);
var defaultFunction = function() {};
this.initialize("done", defaultFunction);
this.initialize("fail", defaultFunction);
this.initialize("always", defaultFunction);
this.isRunning = function() {
return !!params.intervalID;
};
this.run = function() {
if (this.strict && (this.green === false)) {
return;
}
this.green = false;
$.ajax({
url: this.url,
method: this.method,
data: this.data
}).done(function(data, textStatus, jqXHR) {
that.green = true;
that.done(data, textStatus, jqXHR);
}).fail(function(jqXHR, textStatus, errorThrown) {
that.green = true;
that.fail(jqXHR, textStatus, errorThrown);
}).always(function(param1, param2, param3) {
that.green = true;
that.always(param1, param2, param3);
});
};
this.start = function() {
if (!params.intervalID) {
this.run();
params.intervalID = setInterval(this.run.bind(this), this.interval);
}
};
this.stop = function() {
if (!!params.intervalID) {
clearInterval(params.intervalID);
params.intervalID = undefined;
}
};
}
Source: stackoverflow.com
Related Query
- How to add live data to stacked bar chart
- How to create a d3 stacked bar chart that only iterates through columns 1 and 2 of data file?
- how to update text added in dimple.js stacked bar chart on data updation
- D3 horizontal stacked bar chart add and remove data
- D3 How to add rounded top border to a stacked bar chart
- How to add a line at top of stacked bar chart with d3.js?
- D3.js - How to add the general update pattern to a stacked bar chart
- How to add a properly scaled y axis to a stacked d3 bar chart
- D3 Stacked Bar Chart add Data Labels and solving D3 Label Placement
- How to update d3.js bar chart with new data
- d3.js - group 2 data values in a stacked bar chart
- d3.js How to add lines to a bar chart
- How to add a line on x-axis on a horizontal bar chart in d3
- How to add space between bars in a grouped bar chart in a nvd3 grouped multibar chart?
- D3: How can I change data in an existing bar chart
- d3 v4 nested data and stacked bar chart
- How to update data in stack bar chart in D3
- How to modify axis labels in d3 for a stacked bar chart when the axis labels are mapped as part of the scale's domain
- D3.js : How to read data from JSON in Grouped Bar Chart
- D3 V4 Rect bind data in stacked bar chart
- How to make grouped stacked bar chart in d3js?
- d3/dc.js - How to create a stacked bar chart while telling crossfilter to treat elements in an array as separate records?
- How to line up x axis on stacked bar chart with a line overlay in d3.js
- How to create a stacked bar chart using dc.js?
- How to show values stacked bar chart utilizing dc.js and d3.js?
- D3 : How to add labels top of in each bar in grouped bar chart
- Dimple.js - Add data labels to each bar of the bar chart
- how to create a stacked bar chart with images in d3
- Stacked Bar Chart with Time Series Data
- D3 v6: How to zoom stacked bar by selected segment to stretch only selected segment and shrink rest segments (and keep initial chart width)?
More Query from same tag
- getting data to an AngularJS directive for D3.js on Mean.js app
- Basic D3.js: how to use joins within a function?
- D3 Race condition between databinding and drag raise
- NetworkX D3 Force Layout Plugin for mpld3
- dynamically giving input to date format for parsing D3.js
- how to stop event propagation inside <table>?
- How to draw a linear gradient circle by svg?
- Adding additional graphs to NVD3 multiChart, shortens the chart (due to legend) what is the fix?
- Updating chart.ygrids in Billboard.js does not update the class
- SVG Translate Group positioning on window resize with d3
- How to implement D3 hierarchy view
- show the bottom most tick of y axis
- Slicing data in a crossfilter object
- Add Tooltips to Interactive D3 Diagram in D3
- How to assign colors according to a index in d3.js?
- d3 conventional indentation with eslint
- Sum of multiple column values for each row in d3.js
- Issue in fetching data from nested json to construct bar graph in d3
- Radio button selected is always on
- csv data to nested json tree in d3
- Extend D3 chart to add zoom functionality
- Using d3 v5, in node console and WebStorm, can't get d3.csv() to work
- Filtering out just one series in chartSeries in react-d3
- CSV color data not rendering
- d3.js ajax default headers/before send
- my tooltip in d3 won't show up in the right place
- brush on rotated lines using d3 to create zoom effect
- Remove white space in d3 table
- ("svg:rect") element does not appear on the screen when run
- d3.js: How to deal with svg elements that are being appended on the same position, one on top of the other?