Javascript 防止脚本复制?

Javascript 防止脚本复制?,javascript,ajax,Javascript,Ajax,我在Ajax中遇到了这个问题,每次发送数据时,脚本都会被复制,这会使我的音频重叠或播放多次 $.ajax({ url: 'fetch.php', method: 'POST', data: { limit: limit, start: start }, cache: false, success: function (data) { $('#load_quotes_recent').append(data); if (!$

我在Ajax中遇到了这个问题,每次发送数据时,脚本都会被复制,这会使我的音频重叠或播放多次

$.ajax({
    url: 'fetch.php',
    method: 'POST',
    data: { limit: limit, start: start },
    cache: false,
    success: function (data) {
        $('#load_quotes_recent').append(data);

        if (!$.trim(data)) {
            action = 'active';
        } else {
            action = 'inactive';
        }

        if (window.location == 'https://www.example.com/') {
            $('.icon-wrapper').on('click', function () {
                var postid = $(this).data('id');
                $post = $(this);

                $.ajax({
                    url: 'fetch.php',
                    type: 'post',
                    data: {
                        'liked': 1,
                        'postid': postid
                    },
                    success: function (response) {
                        var audio = new Audio('../sounds/pop.mp3');
                        audio.pause();
                        audio.currentTime = 0;
                        audio.play();
                    }
                });
            });
        }
    }
});
我想知道是否有办法防止脚本自身复制,这样音频就不会重叠

我之所以有这样的脚本,是因为如果我将脚本放在
fetch.php
Ajax中,我会认为在我到达末尾后仍然有数据,即使我没有


如果有一种方法可以“替换”相同的脚本而不是复制它们,那就太好了。

为什么不将音频与AJAX分开初始化,然后只在需要时播放呢

var audio = new Audio('../sounds/pop.mp3');

$.ajax({
  url: 'fetch.php',
  method: 'POST',
  data: { limit: limit, start: start },
  cache: false,
  success: function (data) {
    $('#load_quotes_recent').append(data);

    if (!$.trim(data)) {
      action = 'active';
    } else {
      action = 'inactive';
    }

    if (window.location == 'https://www.example.com/') {
      $('.icon-wrapper').on('click', function () {
        var postid = $(this).data('id');
        $post = $(this);

        $.ajax({
          url: 'fetch.php',
          type: 'post',
          data: {
            'liked': 1,
            'postid': postid
          },
          success: function (response) {
            audio.pause();
            audio.currentTime = 0;
            audio.play();
          }
        });
      });
    }
  }
});

为什么不将音频与AJAX分开初始化,然后只在需要时播放呢

var audio = new Audio('../sounds/pop.mp3');

$.ajax({
  url: 'fetch.php',
  method: 'POST',
  data: { limit: limit, start: start },
  cache: false,
  success: function (data) {
    $('#load_quotes_recent').append(data);

    if (!$.trim(data)) {
      action = 'active';
    } else {
      action = 'inactive';
    }

    if (window.location == 'https://www.example.com/') {
      $('.icon-wrapper').on('click', function () {
        var postid = $(this).data('id');
        $post = $(this);

        $.ajax({
          url: 'fetch.php',
          type: 'post',
          data: {
            'liked': 1,
            'postid': postid
          },
          success: function (response) {
            audio.pause();
            audio.currentTime = 0;
            audio.play();
          }
        });
      });
    }
  }
});

谢谢,这很有效!现在我只是偶尔要处理一下,但还是要谢谢你!mp3文件有多大?可能取的时候有点耽搁了。谢谢,这很有效!现在我只是偶尔要处理一下,但还是要谢谢你!mp3文件有多大?也许取回它有点延迟。