开发者

How to call a function repeatedly with 5 seconds delay? (ASP.NET with C#)

I have a some piece of code in Default.aspx.cs file which is written within Page_Load function like this:

protected void Page_Load(object sender, EventArgs e)
{
    //code stars here
    ...
    //code ends here   
}

What i want is 开发者_StackOverflowthat this piece of code will run continously with 5 seconds delay. How can I do this here? Thank You.


Looking at your comments I see your actually building a chat facility, which makes more sense regarding the continous requests every 5 seconds.

I would recommend using ASP.NET Web Methods and AJAX technology to achieve this.

Using jQuery.ajax you can make a request every 5 seconds in javascript to return new chat messages

Something like the following would be a good start for your javascript/jquery:

setInterval(function () {
                    $.ajax({
                        url: "/Chat.asmx"
                            , type: "POST"
                            , contentType: 'application/json; charset=utf-8'
                            , data: '{ ConversationID: "' + ConversationID + '"}'
                            , dataType: 'json'
                            , success: function (data) {

                                //do something with data

                            }
                    });
                }, 5000);

Chat.asmx would then be your Web Method. Look into ASP.NET Web Methods for more info:

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

A quicker method, but not as recommended, would be to use jQuery.load where you could have the chat messages in a repeater on a standalone .aspx page and keep loading this into a div on your page like:

setInterval(function () {
     $(".chatmessages").load("ChatConversation.aspx?id=" + ConversationID);
}, 5000);

See jQuery.load for more info on this


As the others suggest, use ajax and get your data from the clietn every 5 seconds. Here is a small sample which should get you started.

You need to include the jQuery javascript framework for this to work.

Clientside javascript:

// execute this on dom ready
$(document).ready(function () {    
    // call function getData every 5 seconds:
    setInterval("getData()", 5000);
});

// Call an asp PageMethod via an ajax call
function getData() {
    $.ajax({
      type: "POST",
      async: true,
      contentType: "application/json; charset=utf-8",
      url: "YourPage.aspx/GetData",
      // optional post some data 
      // data: JSON.stringify(data),          
      success: dataReceived
    });         
}

// callback function is called, when data is recevied
function dataReceived(data, textStatus, jqXHR) {
    // your data is in data.d

}

On the serverside you have to write a "page method" in your aspx page wich is a static method you can call by an ajax javascript call:

public class YourPage : Page
{

    [WebMethod]
    public static object GetData() {
        // return your data here
        return new {Data1 = ..., Data2 = ...};
    }
}

You can pass data to your page method by parameters. The names of the parameters have to match exactly the names of the json object properties you pass to $.ajax.


From your edit, building a chat application would require more than just the page running a repeated task every 5 seconds.

For a simple chat, you could achieve this using AJAX and the setInterval() method in Javascript, but doing so could tie up all threads with an HTTP server such as Apache.

More prudent chats would make use of a distinct HTTP server created in many technologies, you might look into emerging technologies such as socket.io based on node.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜