使用Hopscotch自动播放游戏并让它在关闭/完成后再也不会播放?

时间:2015-06-12 19:09:17

标签: javascript jquery hopscotch

Hopscotch example tour设置参数并调用hopscotch.startTour(tour);但是关闭" x"也不是"完成"按钮似乎阻止它在页面加载后再次播放。

如何实现?

1 个答案:

答案 0 :(得分:3)

您可以在onend函数上使用localstorage / cookie。

function setCookie(key, value) {
    var expires = new Date();
    expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
    document.cookie = key + '=' + value + ';path=/' + ';expires=' + expires.toUTCString();
};

function getCookie(key) {
    var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
    return keyValue ? keyValue[2] : null;
};

var tour = {
    onEnd: function() {
        setCookie("toured", "toured");
    },
    onClose: function() {
        setCookie("toured", "toured");
    }
};

// Initialize tour if it's the user's first time
if (!getCookie("toured")) {
    hopscotch.startTour(tour);
}