jQuery проверяет cookie каждую секунду - PullRequest
0 голосов
/ 31 мая 2018

Я пытаюсь проверить наличие файлов cookie каждую секунду и изменить стиль отображения для кнопки с изображением.Я изменил стиль отображения на none через OnClientClick, и щелчок запустит загрузку Ashx, чтобы отправить обратно тестовый текстовый файл с файлами cookie, «загруженными», чтобы быть правдой (я должен был убедиться, что файл 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 голосов
/ 31 мая 2018

Обновленный ответ до полностью рабочей версии.

Благодаря @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;';
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...