I’m working on ASP.NET MVC3 with C#.
I want to add some delay between each iteration of for loop.
for(int i=0; i<5; i++)
{
//some code
//add delay here
}
So How can I do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As other answers have said, using
Thread.Sleepwill make a thread sleep… but given that you’re writing a web app, I don’t think it’ll do what you want it to.If the aim is for users to see things happening one second at a time, you’ll need to put the delay on the client side. If you have five one-second delays before you send an HTTP response, that just means the user will have to wait five seconds before they see your result page.
It sounds like you should be doing this with AJAX.
EDIT: As noted in comments, it’s probably worth you looking at the
window.setTimeoutJavascript call. (I don’t “do” Javascript, so I’m afraid I won’t be able to help with any of the details around that.)