So basically I wan’t to get data from server at start and then update it every 2 minutes, but instead it gets first value only after 2 minutes, what can I do about this?
Here is my js markup:
var itemViewModel = {
item: ko.observable().extend({ throttle: 120000 }),
loadcontent: function (getID) {
$.ajax({
url: '/api/item/details/' + getID,
dataType: 'json',
success: function (data) {
itemViewModel.item(data);
}
});
}
};
Maybe this will help, here is a HTML markup:
<div id="item-details-content">
<input type="hidden" id="item-id" value="@id" data-bind=""/>
<div class="item-list" data-bind="init: itemPage.loadcontent(@id), with: itemPage.item">
Conditions
- There need to be and initial call of
loadcontent - Everything must be inside
viewModel
That’s not what the
throttle extender(link) is meant for. UsesetInterval(link) instead.throttleis meant to handle a scenario where you may receive an arbitrary amount of events in a short timespan and you do not want to act upon every single event. E.g. you have a search field with autocomplete functionality that uses a REST-API. You do not want to call the REST-API every single time a user pushes a key. Rather, you’d like to wait a bit until the user is done typing. This is a perfect use case forthrottle.What you are looking for is a way to repeat an action on a predetermined interval. JavaScript has a builtin function just for that and it’s called
setInterval.