简体   繁体   English

将复选框和音频状态保存到本地存储

[英]saving both checkbox and audio state to local storage

I am trying to save to local storage the state of both a checkbox (checked/unchecked) and audio (play/pause).我正在尝试将复选框(选中/未选中)和音频(播放/暂停)的状态保存到本地存储。 The default state of the checkbox is checked and audio is playing (looping) as seen in the code below.复选框的默认状态被选中并且音频正在播放(循环),如下面的代码所示。

So if the checkbox is checked, audio is playing and both should be saved to local storage (checkbox checked and audio playing).因此,如果选中该复选框,则音频正在播放,两者都应保存到本地存储(选中复选框并播放音频)。

Else if the checkbox is unchecked, audio is paused and both should be saved to local storage (checkbox unchecked and audio paused).否则,如果未选中该复选框,则音频将暂停,两者都应保存到本地存储(未选中复选框且音频已暂停)。

The problem I have is with saving the state of the checkbox and audio after it's been checked again.我遇到的问题是在再次检查复选框和音频后保存状态。

Steps in reproducing the issue: 1) uncheck checkbox > audio stops > refresh browser > works 2) check checkbox > audio playing > refresh browser > not working, checkbox unchecked and audio not playing重现问题的步骤:1)取消选中复选框>音频停止>刷新浏览器>有效2)复选框>音频播放>刷新浏览器>不起作用,复选框未选中且音频未播放

Here is the javascript:这是javascript:

if (window.localStorage.getItem("musicState") != null) {
    var music = window.localStorage.getItem("musicState");
    if (music == "true") {
        status = 'pause';
        $("audio").trigger(status);
    }
    else {
        status = 'pause';
        $("audio").trigger(status);
        $("input:checkbox[name='music']").removeAttr("checked");
    }
}


$("input:checkbox[name='music']").click(function () {
    var visMusic = "play";
    if (status == 'pause') {
        status = 'play';

    } else {
        status = 'pause';
    }
    $("audio").trigger(status);

    window.localStorage.setItem("musicState", visMusic)

});

here is the HTML:这是 HTML:

<audio autoplay loop>
    <source src="audio/music.mp3" type="audio/mpeg">
</audio>
<label><input type="checkbox" name="music" checked></label>

In your checkbox's click callback you are setting visMusic as "play".在复选框的单击回调中,您将visMusic设置为“播放”。 You never update it before storing it in localStorage as "musicState".在将它作为“musicState”存储在 localStorage 之前,您永远不会更新它。 This means that no matter what you do, "musicState" is always going to equal "play" .这意味着无论你做什么, "musicState" 总是等于 "play"

In your if-statement at the top (where you are retrieving from localStorage) you are testing if what you stored is "true".在顶部的 if 语句(从 localStorage 检索的位置)中,您正在测试您存储的内容是否为“真实”。 It is not, and never will be, because it is always "play" .它不是,也永远不会是,因为它总是“玩” Your code is always going to your else which pauses the music and unchecks the box.您的代码总是转到您的else ,它会暂停音乐并取消选中该框。

Here is an updated version that should work:这是一个应该可以工作的更新版本:

if (window.localStorage.getItem("musicState") != null) {
    var music = window.localStorage.getItem("musicState");
    if (music == "true") {
        status = 'play';
        $("audio").trigger(status);
    }
    else {
        status = 'pause';
        $("audio").trigger(status);
        $("input:checkbox[name='music']").removeAttr("checked");
    }
}

$("input:checkbox[name='music']").click(function () {
    var visMusic = "false";
    if (status == 'pause') {
        status = 'play';
        visMusic = "true";
    } else {
        status = 'pause';
    }
    $("audio").trigger(status);

    window.localStorage.setItem("musicState", visMusic)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM