【问题标题】:is there a way to clear a function after it executes?有没有办法在执行后清除函数?
【发布时间】:2013-07-03 03:50:43
【问题描述】:

抱歉,如果标题不清楚。我有一个 swiperight 和 swipeleft 事件,当触发时将音频源添加到具有自动播放功能的 html5 音频标签中。因此,如果您向左滑动播放一首歌曲并向右滑动另一首播放..问题是它只能播放一次。如果我向左滑动歌曲播放然后如果我向右滑动什么,再次向左滑动什么,反之亦然。有没有办法重置功能?希望这是有道理的,谢谢!下面是我的代码

<script>
$(document).ready(function() {
    $("body").bind("swiperight", function(event){
        $('#swipe').append(
'<source src="http://a.tumblr.com/tumblr_ljdvcbDIsN1qhk4f9o1.mp3"/>');
    })

    $("body").bind("swipeleft", function(event) {
        $('#swipe').append('<source src="http://www.nerdthegame.com/media/audio/soundtrack/Nintendo-Super-Mario-Theme-Song.mp3" />');
    })
})

【问题讨论】:

  • 这是音频标签的ID

标签: jquery mobile swipe


【解决方案1】:

我相信 swipe 事件处理程序每​​次都会执行,但随后您将 .append() 额外的 &lt;source&gt; 元素添加到您的 &lt;audio&gt; 元素中,这样它最终会包含多个 &lt;source&gt; 元素。尝试先删除之前的&lt;source&gt;,您可以使用.empty() 执行以下操作:

$(document).ready(function() {
    $("body").bind("swiperight", function(event){
        $('#swipe').empty().append(
'<source src="http://a.tumblr.com/tumblr_ljdvcbDIsN1qhk4f9o1.mp3"/>');
    })

    $("body").bind("swipeleft", function(event) {
        $('#swipe').empty().append('<source src="http://www.nerdthegame.com/media/audio/soundtrack/Nintendo-Super-Mario-Theme-Song.mp3" />');
    })
})

然后似乎可以工作(我在演示中没有使用滑动事件,因为我在笔记本电脑而不是手机上进行测试):http://jsfiddle.net/AG3Tu/

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 2016-01-06
    • 2019-11-29
    • 2017-11-29
    • 2011-07-13
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    相关资源
    最近更新 更多