我有以下JavaScript创建cookie,我想在创建30分钟后自动删除此cookie。
function getGeoLocation() {
navigator.geolocation.getCurrentPosition(setGeoCookie);
}
function setGeoCookie(position) {
var cookie_val = position.coords.latitude + "|" position.coords.longitude;
document.cookie = "lat_lng=" + escape(cookie_val);
}
答案 0 :(得分:1)
好的,我可以使用以下内容:
function setGeoCookie(position) {
var cookie_val = position.coords.latitude + "|" + position.coords.longitude;
//declare date and get current date time
var date = new Date();
//add 30 minutes to date
date.setTime(date.getTime() + (30 * 60 * 1000));
//create the expires variable
var expires = "; expires="+date.toGMTString();
//pass the "date" variable to expires properity
document.cookie = 'lat_lng'+"="+cookie_val+expires+"; path=/";
}