编辑:这只发生在firefox中,它在chrome中工作正常。
我的网站上有一个音频播放器(由django提供支持的网站),我想在cookie中存储当前时间,来源和状态(是否正在播放)。因此,当您在播放音乐时刷新页面时,它会在您离开的位置继续播放。我有一个计时器设置为每秒更新cookie track_time
。然而它起作用了:
当您尝试第二次登录网站时,在播放音频时,它不会让您失望。它在我的控制台中说登录发生了,但firefox似乎没有存储会话。当我禁用正在编写cookie的脚本时,它再次起作用。
这是证据:
http://puu.sh/nkqIX/23160ce67e.png
世界上到底发生了什么?我没有收到任何错误,它只是不起作用。
代码: 此片段在此处创建,读取和删除Cookie。
/** Cookies **/
function createCookie(name, value, days) {
'use strict';
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(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) return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
在audio.js中我有以下功能:
// Cookie? //
function audioCookie() {
'use strict';
var track, track_time, track_state;
track = readCookie('track');
track_time = readCookie('track_time');
track_state = readCookie('track_state');
if (track !== null) {
track = JSON.parse(track);
audioCurrent = new AudioTrack(track.src, track.artist, track.title, track.genre, track.type);
audioCurrent.setSrc();
if (track_state === '1') {
audioCurrent.play();
}
setTimeout(function () {
audioCurrent.time(track_time);
audioSetProperties();
audioViewUpdate();
audioElementUpdatePlayButton();
audioAnalyserInitialize();
}, 250);
}
}
和
var audioTimeUpdate = function () {
'use strict';
if (audioSource.paused === false) {
audioSetProperties();
createCookie('track_time', audioSource.currentTime, 360);
setTimeout(function () {
audioTimeUpdate();
}, 500);
}
};
创建包含AudioTrack对象(源,名称等)的cookie
// Play the track that is being viewed
var audioPlayFromView = function () {
'use strict';
audioCurrent = audioFromView;
var audioCurrentString = JSON.stringify(audioCurrent);
createCookie('track', audioCurrentString, 360);
audioCurrent.setSrc();
audioCurrent.play();
if (analyserInitialized === true) {
source.disconnect();
source = context.createMediaElementSource(audioSource);
}
audioViewUpdate();
};