How to conditionally register window.setInterval during onload

1.6k Views Asked by At

I have the following jquery that registers a timer interval to occur every 10 seconds on the webpage whenever an input button called btnExport is clicked.

if ($) {
    $(document).ready(function () {
        $("input[id$='btnExport']").click(function ($e) {
            // javascript timer function
            window.setInterval(ExportProgressCheck, 10000);
        });

        function ExportProgressCheck() {
            $.ajax({
                type: "POST",
                url: "DMZ_Export.aspx/GetExportProgress",
                contentType: "application/json; charset=utf-8",
                data: "",
                dataType: "json",
                success: AjaxSuccess,
                error: AjaxFailed
            });
        }
    });
}

However, under certain scenarios I need the timer interval to begin ticking just as soon as the page load itself is loaded. My problem is that I am not sure how to do this from within the page load event of the codebehind. In theory it would go something like...

protected void Page_Load(object sender, EventArgs e) {
    if (!Page.IsPostBack) {
        if (IsExportInProgress()) {
            // Register the timer interval now!! How do I do this??
            // window.setInterval(ExportProgressCheck, 10000);
            }
        }
    }
}

I tried to accomplish this by registering a startup script, but it doesn't like the script because it doesn't know what ExportProgressCheck is...

ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(cstype, csname1,
    "<script type=\"text/javascript\">window.setInterval(ExportProgressCheck, 10000);</script>",
    false);

Any help on this would be very appreciated! Thanks!

3

There are 3 best solutions below

0
On BEST ANSWER

Pulled this off by going another route. I use jQuery's $get function to hit another aspx page to get the condition value.

if ($) {
    $(document).ready(function () {

        $.get("ExportInProgCheck.aspx", function (response) {
            if (response != '')
                window.setInterval(ExportProgressCheck, 10000);
        });
    });
}

ExportInProgCheck.aspx simply returns 'true' if the condition is met, at which point the window.setInterval gets set.

0
On

you're on the right track.

some points

  1. you don't need to add the <script..> tags when calling RegisterStartupScript, just specify true as last parameter.

  2. it appears that your ExportProgressCheck is not yet defined when window.setInterval line hits. You can verify this by simply looking at your source, to see where the function is defined, and where it is referenced by your assignment call. Possible causes: badly formed js could be breaking. is that function available after your page loaded? can you open a javascript window and call it?

  3. Just noticed that $(document).ready line... yes. that will execute after whole BODY element has been parsed. you need to take out ExportProgressCheck function definition out of that ready clause. that way it is available to your startup scripts

registering startup script should put your call on the bottom of segment. all your scripts should be loaded by than. examine the source to determine what is out of whack

3
On

You are emitting your code within $(document).ready() i.e. that code will execute as soon as the dom is loaded (page loaded).

You need just to call setInterval() outside of click function under certain condition:

if ($) {
    $(document).ready(function () {

        if(condition)
            window.setInterval(ExportProgressCheck, 10000);

        $("input[id$='btnExport']").click(function ($e) {
            // javascript timer function
            window.setInterval(ExportProgressCheck, 10000);
        });

        function ExportProgressCheck() {
            $.ajax({
                type: "POST",
                ...