每秒jQuery检查cookie

时间:2018-05-31 18:06:07

标签: javascript jquery cookies

我试图检查每秒是否存在cookie,并更改图像按钮的显示样式。我已经通过OnClientClick将显示样式更改为,并且点击将触发ashx下载以发回带有cookie的测试txt文件"已下载"是真的(我确保cookie存在)。但是,jQuery似乎没有检查或循环,有人可以检查我的代码有什么问题吗?谢谢!

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataPull.aspx.cs" Inherits="DataPullForDemandPlanning.DataPull" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Data Pull For Demanding Planning</title>
    <style>
        .changeVisible{
            display: inherit;
        }
    </style>
    <script type="text/javascript" src="Scripts/jquery-2.1.0.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery.cookie.js"></script>
    <script type="text/javascript"> 
        function changeVisibility(stat) {
            var element = document.getElementById('ibtnPullData');
            if (stat == 'none')
                element.style.display = 'none';
            else
                element.style.display = 'inherit';
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <table style="width: 100%">
            <tr>
                <td align="center" style="border-bottom-color: #990000; height: 30px; border-bottom-style: solid" valign="top">
                    <h2>Data Pull For Demand Planning</h2>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Logged in as: <asp:Label ID="lblUser" runat="server" Visible="true"></asp:Label>
                </td>
            </tr>
        </table>
        <table style="width: 60%">
            <tr>
                <td style="width: 40px">
                    <asp:ImageButton ID="ibtnPullData" runat="server" CssClass="changeVisible" ImageUrl="~/Images/ExcelLogo.gif" OnClick="ibtnPullData_Click" OnClientClick="javascript:changeVisibility('none');" />
                </td>
                <td style="width: auto">
                    Click Here to start data pull. The process may take up to 1 minute.
                </td>
            </tr>
        </table>
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $.removeCookie('downloaded', { path: '/' });

            jQuery.fn.timer = function () {
                var value = $.cookie('downloaded');
                if (value == 'true') {
                    changeVisibility('inherit');
                    $.removeCookie('downloaded', { path: '/' });
                }
            }

            window.setInterval(function () {
                $("changeVisible").timer();
            }, 1000);
        });
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

更新了完整工作版本的答案。

感谢@Barmar,我只能通过JavaScript轻松实现我的目标。这是我的代码。

function changeVisibility(stat) {
    deleteCookie('downloaded');
    var element = document.getElementById('ibtnPullData');
    if (stat == 'none')
        element.style.display = 'none';
    else
        element.style.display = 'inherit';
}

var tim = setInterval(chechCookie, 1000);
function chechCookie() {
    var myCookie = getCookie("downloaded");

    if (myCookie != null) {
        changeVisibility('inherit');
    }
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else {
        begin += 2;
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
            end = dc.length;
        }
    }
    // because unescape has been deprecated, replaced with decodeURI
    //return unescape(dc.substring(begin + prefix.length, end));
    return decodeURI(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name) {
    document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}