Please let me know how to add "delay" for "setTimeout" in angular 6 with forEach loop
to execute each loop after specific interval.
Below is my code.
selectedPlans.forEach(async (selectedPlan) => {
this.makePayment(selectedPlan)
});
In above code "makePayment" needs to be call after specific interval.
Thanks
Related
I have an Xamarin.iOS app which does some search on a long list that takes some time.I would like to introduce some throttling instead of searching for every single key stroke . Any idea ?
You can use Reactive extensions, asuming that you use C# style events instead of iOS Delegates.
Here is a bare bone sample code with UITextView:
var observable = Observable.FromEventPattern<EventArgs>
(
//Those are actions used to subscribe and unsubscribe for the event
eventHandler => TextView.Changed += eventHandler,
eventHandler => TextView.Changed -= eventHandler
)
.Sample(TimeSpan.FromMilliseconds(500)) //This will sample every 500 to check if the event has been raised
.Select(e => e.Sender as UITextView) //This is used so we can directly use the TextView in the subscribe method
observable.Subscribe(textView =>
{
//This will be executed if the event was raised the last 500 miliseconds
//This may not run on the UI thread
InvokeOnMainThread(() => Label.Text = textView.Text);
});
This will Sample the text every 500 miliseconds and if the event was rased at least once it will call the Action in the Subscribe method. Or you can use .Throttle instead of .Sample which will be called if the event was not raised for the interval which will be 500 miliseconds after the user stopped typing.
The cool thing is that you can filter the text with a:
.Where(textView => !String.IsNullOrWhiteSpace(textView.Text) || !IsCompleteNonSense(textView.Text));
after the Select method so it wont raise the event if the text is empty or if you want to make some other checks.
I came across this watching a presentation from Xamarin Evolve 2016 here is a link to the video. And here is a link to the source code.
The samples are on Xamarin.Forms but they will give you a good starting point.
In the ASP MVC page I'm currently working on, the values of three input fields determine the value of a fourth. Zip code, state code, and something else called a Chanel Code will determine what the value of the fourth field, called the Territory Code, will be.
I just started learning jQuery a couple weeks ago, so I would first think you could put a .change event that checks for values in the other two fields and, if they exists, call a separate method that compares the three and determines the Territory code. However, I'm wondering if there is a more elegant way to approach this since it seems like writing a lot of the same code in different places.
You can bind a callback to multiple elements by specifying multiple selectors:
$(".field1, .field2, .field3").click(function() {
return field1 +
field2 +
field3;
});
If you need to perform specific actions depending on which element was clicked, another option would be to create a function which performs the actual computation and then invoke that from each callback.
var calculate = function() {
return field1 +
field2 +
field3;
};
And then invoke this function when on each click:
$(".field1").click(function() {
// Perform field1-specific logic
calculate();
});
$(".field2").click(function() {
// Perform field2-specific logic
calculate();
});
// etc..
This means that you do not repeat yourself.
This works for me
jQuery(document).on('scroll', ['body', window, 'html', document],
function(){
console.log('multiple')
}
);
Adding another possibility, just in cased this may help someone. This version should work on dynamically created fields.
$("#form").on('change', '#Field1, #Field2, #Field3', function (e) {
e.preventDefault();
console.log('something changed');
});
I'd like to have just a part of my page updated every second. I know I can get this to work using setInterval in JavaScript but what if I want some logic built into my Rails code? Here's a simple abstraction:
Code in controller:
def lookup_something
#stuff_to_display = [code to look up a set of records]
end
Then in the view:
<body>
<div id="update_this"></div>
</body>
I'd like something to run that action every second and update the div above. I know I've done this before in a previous project with a relatively simple block of code but I can't find out how to do it again for the life of me.
If you are willing to use jQuery, the $.ajax call is fairly straightforward:
From http://api.jquery.com/jQuery.ajax/
setInterval(function() {
$.ajax({
url:"url/to/lookup_something",
success:function(result){
// do something here to
// $("#update_this")
}
});
}, 1000); // every second
I'm using SmartGWT 2.5.
I have a main grid that has its expandable rows in order to display subgrids.
I simply want to display the main grid with all its rows expanded from the start.
I tried to add a listener containing the following code:
ListGridRecord[] records = getRecords();
for (ListGridRecord rec : records) {
expandRecord(rec);
}
I tried with DataArrivedHandler and DrawAreaChangedHandler, but I just get javascript errors client-side or only parts of the rows are expanded. How can I fix this?
If you are talking about Grid Grouping, then you can use the following:
grid.setGroupStartOpen(GroupStartOpen.ALL);
listGrid.addDataArrivedHandler(new DataArrivedHandler() {
#Override
public void onDataArrived(DataArrivedEvent event) {
for (ListGridRecord rec : listGrid.getRecords()) {
listGrid.expandRecord(rec);
}
}
});
Should work (worked with previous versions..)
What error do you get?
Ok finally, I've put a timer of 100 ms within each handler.
The issue was that there was a delay before the full creation of the components (what I want to display is quite complex), and so when the handler was called, not everything was in place yet...
I am running Backbone js 0.9.2 on Rails 3.2.2,I have a page for adding cost rows.A cost have 3 TextFields: title, description and price.
I am saving each cost on blur.
model.save() gets called multiple times with very short intervals. Which issues one create(post) request then one update(put) request shortly there after. The problem I am experiencing is that PUT request sometimes reaches the server before the POST, the result being that model gets created and persisted twice(duplicates).
To save on blur is the requested behavior, so I need a way to queue up requests.
I have read something about Spine js, and that they solve it by some kind of queue. I've also looked in to this, but can't seem to figure this out.
It feels like this should be a common issue, working with "single-page-apps" but can't find anything about it.
You could override the save method and create a queue with a deferred object . For example,
var MDef = Backbone.Model.extend({
url: "/echo/json/?delay=3",
initialize: function() {
this.queue = $.Deferred();
this.queue.resolve();
},
save: function(attrs,options) {
var m = this;
console.log("set "+JSON.stringify(attrs));
// this.queue = this.queue.pipe with jquery<1.8
this.queue = this.queue.then(function() {
console.log("request "+JSON.stringify(attrs));
return Backbone.Model.prototype.save.call(m, attrs, options);
});
}
});
var m = new MDef();
m.save({title: "a title"});
m.save({description: "a description"});
m.save({price: "a price"});
And a Fiddle : http://jsfiddle.net/nikoshr/8nEUm/
User debounce from underscore.js.
Creates and returns a new debounced version of the passed function that will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked.
This way it will only fire once after the last blur event.