简体   繁体   English

写入document.cookie需要多长时间

[英]How long does it take to write to document.cookie

Was wondering how long it takes to write to document.cookie. 想知道写入document.cookie需要多长时间。 Ran into an edge case when setting a cookie and re-directing to another page and the document.cookie was not getting set. 设置cookie并重定向到另一个页面时,遇到极端情况,未设置document.cookie。 Seemed to have different performance on a desktop (Chrome, firefox) vs iphone/tablet (safari) 似乎在台式机(Chrome,Firefox)和iPhone /平板电脑(Safari)上具有不同的性能

Seemed to worked correctly in all cases when I added a set timeout of about 500ms 当我添加约500ms的设置超时时,似乎在所有情况下都能正常工作

// writing cookie out //写出cookie

   function set_cookie(name, value ) {
        var date =  new Date();
        date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000)); 
        var cookie = name + "=" + value + "; expires=" + date.toGMTString() + ";";
        document.cookie = cookie;

    }

// reading cookie //阅读cookie

function read_Cookie(name) {

        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) {
                var cVal = c.substring(nameEQ.length, c.length);
                return cVal;
            }
        }
        return null;
    }

// button click setting cookie and navigation //按钮单击设置cookie和导航

        $("#goToWebButton").click(function() {
             if ($('#chkDontShow').attr('checked') == 'checked') {
                set_cookie('IgnoreInAppLink','web');
               }
            setTimeout(function(){window.location.href = '';}, 500);
        });

        $("#goToAppButton").click(function() {
          if ($('#chkDontShow').attr('checked') == 'checked') {
                set_cookie('IgnoreInAppLink','app');
            }
            setTimeout(function(){window.location.href = '';},500);

        });

JavaScript is by it's very nature single-threaded therefore the redirect shouldn't be executed until the cookie has been written regardless of how long it takes (almost instantaneous). JavaScript本质上是单线程的,因此无论写了多长时间(几乎是瞬时的),都应该在cookie写入后才执行重定向。 That said, as pointed out at Is JavaScript guaranteed to be single-threaded? 就是说,正如在上所指出的那样, JavaScript是否可以保证是单线程的? that may not always be the case. 并非总是如此。 To mitigate this you might consider using jQuery's deferred object as such: 为了减轻这种情况,您可以考虑使用jQuery的延迟对象,如下所示:

function set_cookie(name,value,deferred) { // added deferred argument
    var date =  new Date();
    date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000)); 
    var cookie = name + "=" + value + "; expires=" + date.toGMTString() + ";";
    document.cookie = cookie;
    deferred.resolve();
}

var deferred = $.Deferred();

deferred.done(function() {
    window.location.href = "";
});

$("#goToAppButton").click(function() {
    if ($("#chkDontShow").attr("checked") == "checked") {
        set_cookie("IgnoreInAppLink","web",deferred);
    } else {
        deferred.resolve();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM