2017-05-23 26 views
0

我已经搜索了答案,但似乎找不到答案。 我的计划是在添加cookie后显示警报。Javascript - 在cookie上做些什么添加

我在setTimeout 5秒后设置了cookie。

脚本

setTimeout(
    function(){ 
     document.cookie = "username=Billy Joe"; 
    }, 5000); 


我的做法是我使用此代码检查cookie存在与否。

if(!getCookie("username")){ 
    alert("Cookie doesn't exists"); 
}else{ 
    alert("Cookie exists"); 
} 


但看起来就像是只检查第一负载,在添加cookie之后没有。
我的问题是如何在cookie添加后显示警报?
谢谢你这么多

+0

你必须更精确地在你的问题。如果您设置了Cookie,则您已知道您的Cookie已设置。所以,对我来说,在'setTimeout'函数中添加一个检查是没有意义的。如果你想有一些平行性,例如,您可以独立检查您的Cookie是否已设置,我建议使用事件或承诺来处理异步调用。 – Guybrush

+0

@Guybrush感谢您的建议。下次我会更精确地提出一个问题。是的,在超时时间内检查cookie是没有意义的,因为我可以随时检查cookie。我的cookie是从php文件中添加的,所以我想在超时功能之外检查cookie。诚然,我想单独检查它,但似乎我无法从我搜索的来源找到答案。你能分享一些事件来独立处理异步调用/在超时功能之外吗?非常感谢Guy! – Billy

回答

-1

不幸的是,在这种情况下,我发现一个错误的API,因为这个API是Mozilla网络扩展。注意它。唯一的工作方法是使用setInterval函数(参见https://www.experts-exchange.com/questions/22641766/Javascript-Call-function-as-soon-as-cookie-value-changes-Set-listener-for-changed-cookie-value.html)。不过,我更喜欢这样做,因为它没有任何意义不设置函数中的cookie):

var handledCookie = false;  // Was the cookie already handled? 
var cookieCheckId = undefined; // The id of the checker 

var handleCookie = function() { 
    if (!getCookie("username")){ 
    alert("Cookie doesn't exists"); 
    } else { 
    clearInterval(cookieCheckId); // Unset the check 
    handledCookie = true;   // Set the cookie handled 
    alert("Cookie exists"); 
    } 
}; 

handleCookie(); 

if (!handledCookie) { 
    cookieCheckId = setInterval(handleCookie, 5000); // Handle the cookies 
} 

所以,如果cookie是内部或外部的代码创建的,该handleCookie函数将被调用一次。检查完成5秒钟。如果找到该cookie,则该检查将被禁用(clearInterval)。

错!!!回答之前

您可以处理事件。如果cookie发生变化,则会触发事件(参见https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/cookies/onChanged)。

例如,你可以使用这样的事情:

browser.cookies.onChanged.addListener(function(changeInfo) { 
    if(!getCookie("username")){ 
    alert("Cookie doesn't exists"); 
    } else { 
    alert("Cookie exists"); 
    } 
}); 

你也可以采取在changeInfo一看,并使用该信息来识别你的cookie:

browser.cookies.onChanged.addListener(function(changeInfo) { 
    let cookie = JSON.stringify(changeInfo.cookie); 
    if (cookie.name === 'username') { 
    alert("Cookie exists"); 
    } 
}); 

但是,那么你没有检查你的cookie是否不存在。

+0

嗨Guybrush! 非常感谢您的回答。 我用你的代码,不幸的是我得到这个错误 “TypeError:browser.cookies is undefined” – Billy

+0

对不起。这似乎是一个新的API,并不适用于所有浏览器。我看你的。 – Guybrush

+0

不幸的是,该API用于网络扩展。所以他们不在普通网站上工作。但是,我已经更正了我的答案,现在它应该可以工作。没有事件或别的东西来处理cookies。 – Guybrush

0

My question is how to show the alert after cookie has been added?

非常容易,只要做你检查你所添加的饼干后!

function checkCookie() { 
    alert(getCookie("username") ? "Cookie exists!" : "Cookie doesn't exist!"); 
} 

setTimeout(function() { 
    document.cookie = "username=Billy Joe"; 
    checkCookie(); 
}, 5000); 
+0

你好亚历克斯!谢谢您的回答。对此,我真的非常感激。但是我忘了提到checkCookie()函数必须超时,因为cookie是从php创建的,而不是超时。那么如何实现这一目标?谢谢! – Billy

0

基本上setTimeout总是异步运行,所以基本上你的if块在设置cookie值之前运行。添加cookie后应该使用函数。例如你的超时功能运行/设置5秒钟后的cookie值,但你的修改立即读取setTimeout代码

这里是例子是应该为你工作。

function checkCookie(){ 
    if(!getCookie("username")){ 
     alert("Cookie doesn't exists"); 
    }else{ 
     alert("Cookie exists"); 
    } 
} 

// check before add 
checkCookie() 

setTimeout(function(){ 
    document.cookie = "username=Billy Joe"; 
    // check after add 
    checkCookie(); 
}, 5000); 
+0

我不明白为什么我应该检查cookie时,它是由我自己设置在相同的代码中? – Guybrush

+0

你可以重命名函数brother。checkCookie来改变cookie。 –

+0

好的,如果函数在代码中的其他地方被重用并且有用的是由它完成的,那么它可能是有意义的,但是在这种情况下,最好是使用一个额外的函数来处理'true'的情况,不是吗?然后你可以直接在'setTimeout'中执行额外的函数,对吗? – Guybrush