0

我正在使用 Matt Schmidt 的 jQuery Timer 插件

/*
*
*  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);
};

我在以下链中使用它:

$(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);
                    }
                });
        });
    });

我的问题是,ajax 调用实际上只执行一次,我希望每次达到计时器间隔时都执行一次。我曾尝试在调用 reset() 之前删除对 stop() 的调用,但这没有区别。

我错过了一些明显的东西吗?

编辑:我认为这并不重要,但这是我控制器上的方法:

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);

        }
4

1 回答 1

1

您的时间间隔设置为每 10 秒运行一次,而不是您的评论指出的每秒运行一次。我已将毫秒数更新为正确的 1,000,并在此处创建了一个 jsFiddle:

http://jsfiddle.net/ySHUQ/

一切似乎都正常工作(显然每秒都会收到错误警报,因为 URL 不存在)。也许你只是没有等待足够长的时间?

于 2012-06-07T11:09:12.353 回答