在rails应用程序中使用Javascript,在创建30分钟后删除cookie

时间:2018-01-17 15:44:04

标签: javascript

我有以下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);
}

1 个答案:

答案 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=/";

}