运行JQuery函数

时间:2015-10-28 16:53:33

标签: jquery

我有一个.js文件,我试图与我当前的网站合并。它被设计为在经过一段时间后,向用户显示一个对话框,可以选择立即注销或保持连接。我在线获取了.js文件,似乎无法理解其使用方向。方向如下

//使用

  1. 包含jQuery
  2. 包含jQuery UI(用于对话框)
  3. 包括jquery.sessionTimeout.js
  4. 调用$ .sessionTimeout();文件准备好后
  5. 这里是带有文档的jquery.sessionTimeout.js。

    /*jshint browser:true*/
    
    //
    // jquery.sessionTimeout.js
    //
    // After a set amount of time, a dialog is shown to the user with the option
    // to either log out now, or stay connected. If log out now is selected,
    // the page is redirected to a logout URL. If stay connected is selected,
    // a keep-alive URL is requested through AJAX. If no options is selected
    // after another set amount of time, the page is automatically redirected
    // to a timeout URL.
    //
    //
    // USAGE
    //
    //   1. Include jQuery
    //   2. Include jQuery UI (for dialog)
    //   3. Include jquery.sessionTimeout.js
    //   4. Call $.sessionTimeout(); after document ready
    //
    //
    // OPTIONS
    //
    //   message
    //     Text shown to user in dialog after warning period.
    //     Default: 'Your session is about to expire.'
    //
    //   keepAliveUrl
    //     URL to call through AJAX to keep session alive. This resource should    do something innocuous that would keep the session alive, which will depend on your server-side platform.
    //     Default: '/keep-alive'
    //
    //   keepAliveAjaxRequestType
    //     How should we make the call to the keep-alive url? (GET/POST/PUT)
    //     Default: 'POST'
    //
    //   redirUrl
    //     URL to take browser to if no action is take after warning period
    //     Default: '/timed-out'
    //
    //   logoutUrl
    //     URL to take browser to if user clicks "Log Out Now"
    //     Default: '/log-out'
    //
    //   warnAfter
    //     Time in milliseconds after page is opened until warning dialog is opened
    //     Default: 900000 (15 minutes)
    //
    //   redirAfter
    //     Time in milliseconds after page is opened until browser is redirected to redirUrl
    //     Default: 1200000 (20 minutes)
    //
    //   appendTime
    //     If true, appends the current time stamp to the Keep Alive url to prevent caching issues
    //     Default: true
    //
    (function ($) {
        jQuery.sessionTimeout = function (options) {
            var defaults = {
                message: 'Your session is about to expire.',
                keepAliveUrl: '/keep-alive',
                keepAliveAjaxRequestType: 'POST',
                redirUrl: '/timed-out',
                logoutUrl: '/log-out',
                warnAfter: 900000, // 15 minutes
                redirAfter: 1200000, // 20 minutes
                appendTime: true // appends time stamp to keep alive url to prevent caching
            };
    
        // Extend user-set options over defaults
        var o = defaults,
                dialogTimer,
                redirTimer;
    
        if (options) { o = $.extend(defaults, options); }
    
        // Create timeout warning dialog
        $('body').append('<div title="Session Timeout" id="sessionTimeout-dialog">' + o.message + '</div>');
        $('#sessionTimeout-dialog').dialog({
            autoOpen: false,
            width: 400,
            modal: true,
            closeOnEscape: false,
            open: function () { $(".ui-dialog-titlebar-close").hide(); },
            buttons: {
                // Button one - takes user to logout URL
                "Log Out Now": function () {
                    window.location = o.logoutUrl;
                },
                // Button two - closes dialog and makes call to keep-alive URL
                "Stay Connected": function () {
                    $(this).dialog('close');
    
                    $.ajax({
                        type: o.keepAliveAjaxRequestType,
                        url: o.appendTime ? updateQueryStringParameter(o.keepAliveUrl, "_", new Date().getTime()) : o.keepAliveUrl
                    });
    
                    // Stop redirect timer and restart warning timer
                    controlRedirTimer('stop');
                    controlDialogTimer('start');
                }
            }
        });
    
        function controlDialogTimer(action) {
            switch (action) {
                case 'start':
                    // After warning period, show dialog and start redirect timer
                    dialogTimer = setTimeout(function () {
                        $('#sessionTimeout-dialog').dialog('open');
                        controlRedirTimer('start');
                    }, o.warnAfter);
                    break;
    
                case 'stop':
                    clearTimeout(dialogTimer);
                    break;
            }
        }
    
        function controlRedirTimer(action) {
            switch (action) {
                case 'start':
                    // Dialog has been shown, if no action taken during redir period, redirect
                    redirTimer = setTimeout(function () {
                        window.location = o.redirUrl;
                    }, o.redirAfter - o.warnAfter);
                    break;
    
                case 'stop':
                    clearTimeout(redirTimer);
                    break;
            }
        }
    
        // Courtesy of http://stackoverflow.com/questions/5999118/add-or-update-query-string-parameter
        // Includes fix for angular ui-router as per comment by j_walker_dev
        function updateQueryStringParameter(uri, key, value) {
            var re = new RegExp("([?|&])" + key + "=.*?(&|#|$)", "i");
    
            if (uri.match(re)) {
                return uri.replace(re, '$1' + key + "=" + value + '$2');
            } else {
                var hash = '';
    
                if (uri.indexOf('#') !== -1) {
                    hash = uri.replace(/.*#/, '#');
                    uri = uri.replace(/#.*/, '');
                }
    
                var separator = uri.indexOf('?') !== -1 ? "&" : "?";
                return uri + separator + key + "=" + value + hash;
            }
        }
    
        $(document).ajaxComplete(function () {
            if (!$('#sessionTimeout-dialog').dialog("isOpen")) {
                controlRedirTimer('stop');
                controlDialogTimer('stop');
                controlDialogTimer('start');
            }
        });
    
        // Begin warning period
        controlDialogTimer('start');
    };
    })(jQuery);
    

    我还是JQuery的新手,不要放弃了解如何实现它。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要在文档的>>> import datetime >>> url_date = "2015-01-12T08:43:02Z" >>> datetime.datetime.strptime(url_date , '%Y-%m-%dT%H:%M:%SZ') datetime.datetime(2015, 1, 12, 8, 43, 2) 中输入以下内容:

<HEAD>

这是使用Google CDN for jQuery组件,它假设您的<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <script src="sessionTimeout.js"></script> <script> // A $( document ).ready() block. $( document ).ready(function() { console.log( "ready!" ); $.sessionTimeout(); }); </script> 文件与HTML文件位于同一位置。

您可以将脚本放在其他地方,例如靠近HTML的底部,但这是另一个讨论。将它放在sessionTimeout.js中是一种简单的方法。