0

I am using Matt Schmidt's jQuery Timer plugin

/*
*
*  jQuery Timer plugin v0.1
*    Matt Schmidt [http://www.mattptr.net]
*
*  Licensed under the BSD License:
*    http://mattptr.net/license/license.txt
*
*/

jQuery.timer = function (interval, callback) {
    /**
    *
    * timer() provides a cleaner way to handle intervals  
    *
    *  @usage
    * $.timer(interval, callback);
    *
    *
    * @example
    * $.timer(1000, function (timer) {
    *   alert("hello");
    *   timer.stop();
    * });
    * @desc Show an alert box after 1 second and stop
    * 
    * @example
    * var second = false;
    *  $.timer(1000, function (timer) {
    *    if (!second) {
    *      alert('First time!');
    *      second = true;
    *      timer.reset(3000);
    *    }
    *    else {
    *      alert('Second time');
    *      timer.stop();
    *    }
    *  });
    * @desc Show an alert box after 1 second and show another after 3 seconds
    *
    * 
    */

    var interval = interval || 100;

    if (!callback)
        return false;

    _timer = function (interval, callback) {
        this.stop = function () {
            clearInterval(self.id);
        };

        this.internalCallback = function () {
            callback(self);
        };

        this.reset = function (val) {
            if (self.id)
                clearInterval(self.id);

            var val = val || 100;
            this.id = setInterval(this.internalCallback, val);
        };

        this.interval = interval;
        this.id = setInterval(this.internalCallback, this.interval);

        var self = this;
    };

    return new _timer(interval, callback);
};

I use it in the following chain:

$(document).ready(function () {
        // Get the requestId
        var requestId = $('#RequestId').val();
        var timerInterval = 10000;
        // On document load create a timer which will poll the controller every second
        $.timer(timerInterval, function (timer) {
            $.ajax(
                {
                    type: 'GET',
                    url: "/CreateFIS/GetRequestStatus",
                    data: { strRequestId: requestId },
                    success:
                        function (response) {
                            alert(response);
                            if (response != null) {
                                switch (response.toString()) {
                                    case 'Pending':
                                        // The request is still currently awaiting processing
                                        $('#InformationSpan').html('The request is currently Pending. Please Wait...');
                                        timer.stop();
                                        timer.reset(timerInterval);
                                        break;
                                    case 'Cancelled':
                                        // The request has been cancelled
                                        $('#InformationSpan').html('The request has been cancelled.');
                                        timer.stop();
                                        break;
                                    case 'Error':
                                        // There was an error with the request
                                        $('#InformationSpan').html('There has been an error generating the request. Please contact the administrator regarding this error.');
                                        break;
                                    case 'In Progress':
                                        // Generation is in progress
                                        $('#InformationSpan').html('The request is currently in progress, and will be with you shortly. Please Wait...');
                                        timer.stop();
                                        timer.reset(timerInterval);
                                        break;
                                    case 'Generated':
                                        // Generation has finished
                                        $('#InformationSpan').html('The request has finished generating.');
                                        timer.stop();
                                        //Todo redirect to viewer
                                        break;
                                    default:
                                        break;
                                }
                            }
                        },
                    error: function (req, status, error) {
                        alert(req);
                        alert(status);
                        alert(error);
                    }
                });
        });
    });

My problem is, the ajax call is only actually performed once, where I want it to be perfromed each time the timers interval is hit. I have tried removing the calls to stop() before the call to reset(), however this makes no difference.

Am I missing something obvious?

Edit: I dont think this really matters, but here is the method on my controller:

public JsonResult GetRequestStatus(string strRequestId)
        {
            Guid requestId;
            bool result = Guid.TryParse(strRequestId, out requestId);
            if(!result)
            {
                // TODO handle error here
                return null;
            }
            FISServiceClient fisServiceClient = new FISServiceClient();
            var status = fisServiceClient.GetFISRequestStatus(requestId);
            if (status == null) return null;
            return Json(status.FISRequestStatusName, JsonRequestBehavior.AllowGet);

        }

1 Answer 1

1

Your interval is set to run every 10 seconds, not every second as your comment indicates. I've updated the number of milliseconds to the correct 1,000, and created a jsFiddle here:

http://jsfiddle.net/ySHUQ/

Everything seems to work properly (obviously receiving the error alerts every second, since the URL doesn't exist). Perhaps you just weren't waiting long enough?

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry Ben, I set the interval to 10 seconds so I could test. The timer fires fine, however the call to the url is not made each time the timer fires for some reason.
OK. I don't quite understand what you mean? In the fiddle I posted, I receive a series of alerts every second (as fired in the error function for the AJAX call. That would indicate there's nothing wrong with the AJAX call. Did I misunderstand?
Of course you receive the error, you dont have the method locally. When debugging here, I can see the method is executed and a value is returned. The method is not called every time the timer fires.
I fully understand why I receive the error, that's not in question. My point is, this indicates that the ajax() function is being called every second. If the method isn't being called when the AJAX function is run, there must be a problem somewhere else. Your original statement was that the AJAX call was 'only actually performed once', which, as evidenced by the error function, isn't true...
Thanks Ben, the issue was actually in the service the controller called.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.