【问题标题】:if/else with setInterval auto-refreshing and on click refreshing event jquery?if/else 与 setInterval 自动刷新和点击刷新事件 jquery?
【发布时间】:2020-06-27 17:06:28
【问题描述】:

努力让它正常工作...使用 setInterval 创建一个 if/else 语句,点击 if 类,内容刷新,else 内容在特定时间段后自动刷新。这就是我自动刷新 atm 所拥有的(效果很好):

var auto_refreshContentTwo = setInterval (
    function() {
        $('.page_loading_r_content_two_overlay').fadeIn();
        $.ajax({
            url: '../../path/to/page.php',
            success: function(html) {
                var myContentTwoContent = $('#refreshContentTwo').html(html).find('#refreshContentTwo2');
                $('#refreshContentTwo').html(myContentTwoContent);
            }
        });
    }, 495000
);

我试图添加“点击”功能,但没有做任何事情......:

$('.contentTwoClicked').on('click', function() {
    var refreshClicked = true;
    if(refreshClicked) {
        alert('clicked');
        $('.page_loading_r_content_two_overlay').fadeIn();
        $.ajax({
            url: '../../path/to/page.php',
            success: function(html) {
                var myContentTwoContent = $('#refreshContentTwo').html(html).find('#refreshContentTwo2');
                $('#refreshContentTwo').html(myContentTwoContent);
            }
        });
    } else {
        var auto_refreshContentTwo = setInterval (
            function() {
                $('.page_loading_r_content_two_overlay').fadeIn();
                $.ajax({
                    url: '../../path/to/page.php',
                    success: function(html) {
                        var myContentTwoContent = $('#refreshContentTwo').html(html).find('#refreshContentTwo2');
                        $('#refreshContentTwo').html(myContentTwoContent);
                    }
                });
            }, 495000
        );
    }
});

我哪里错了?或者我在这里离基地很远......?任何指导/帮助将不胜感激!

【问题讨论】:

  • 你也可以添加html吗
  • 让我们看看你的逻辑。您希望在未单击“类”时发生某些事情。单击“类”时,您还想要一些东西。因此,为什么您的“自动”代码在 click 事件处理程序中?
  • @GetSet,我不确定你的意思……auto_refreshContentTwo 代码在 else 语句中,我认为如果点击事件没有被点击……?跨度>
  • 像这样使用$(document).on('.contentTwoClicked','click', function() { ... });。因为您在页面加载后附加了新的 HTML 内容。
  • 你是刷新整个页面,还是特定节点?

标签: javascript php html jquery css


【解决方案1】:

您不需要条件语句,而是需要一个变量来存储设置的间隔,以便可以通过调用函数在手动刷新时将其清除并重新启动:

//variable to store the setInterval
let refreshInterval = '';

//function that calls setInterval
const autoRefresh = () => {
  refreshInterval = setInterval(()=> { 
    refresh();
    console.log("auto");
  },3000)
}

//run setInterval function on page load; 
autoRefresh();

//manual refresh function
const manualRefresh = () => {
  //erases the setInterval variable
  clearInterval(refreshInterval);
  refresh();
  //then recalls it to reset the countdown
  autoRefresh();
  console.log("manual");
}

//for visual purposes
let refreshCount = 0; 

//node refresher function
const refresh = () => {
    const container = document.getElementById("refresh");
    refreshCount ++; 
    container.textContent= "This div will be refreshed"+ ` Refresh Count: ${refreshCount}`; 
}

<button onclick="manualRefresh()">Click to Refresh </button>

<div id="refresh">This div will be refreshed</div>

查看实际操作:https://codepen.io/PavlosKaralis/pen/rNxzZjj?editors=1111

编辑:应用于您的代码,我认为是:



let interval; 

var autoRefresh = () => {
  interval = setInterval (
  function() {
      $('.page_loading_r_content_two_overlay').fadeIn();
      $.ajax({
          url: '../../path/to/page.php',
          success: function(html) {
              var myContentTwoContent = $('#refreshContentTwo').html(html).find('#refreshContentTwo2');
              $('#refreshContentTwo').html(myContentTwoContent);
          }
      });
  }, 495000);
}

$('.contentTwoClicked').on('click', function() {
  
  clearInterval(interval);
  
  alert('clicked');
  $('.page_loading_r_content_two_overlay').fadeIn();
  $.ajax({
      url: '../../path/to/page.php',
      success: function(html) {
          var myContentTwoContent = $('#refreshContentTwo').html(html).find('#refreshContentTwo2');
          $('#refreshContentTwo').html(myContentTwoContent);
      }
  });

  autoRefresh(); 

});

autoRefresh(); 

【讨论】:

  • 我会试一试 - 从你的演示中,这正是我正在寻找的......我会及时通知你。
  • 我更新了我的答案以反映它将如何应用于您的代码,但由于我没有完整的代码,因此无法确定。
  • @PavlosKaralis 我会发表评论,因为我认为 OP 可能缺少关键点。根据您的解释,计时器应该停止并重新启动,以便在手动期间不会发生自动。然而,做同样事情的两个函数并不重要,因为无论如何刷新都会及时再次发生。但这将是一个很好的做法。然而,这并不能阻止相反的情况:当点击事件进入时,自动已经在进行中。代码设计本身以消除该区域,可能在 OP 上存在缺陷
  • @PavlosKaralis,到目前为止,有迹象表明一切都在按预期工作!为了确定,仍在测试一些东西。一旦完成所有测试并且一切正常,我会将您的答案标记为已解决。我确实必须更改一些内容,例如:interval = setTimout { 你有一个错字,但即使使用setTimeout,事情也没有按预期工作,所以将其更改为setInterval
  • 我还需要实现@GhanuBha 在他对我的问题的评论中所说的内容,但这也有点偏离 - 将其从 $(document).on('.contentTwoClicked','click', function() { ... }); 更改为 $(document).on('click', '.contentTwoClicked', function() { ... }); 并在我们有 @987654330 的地方实施@ 作为内容被动态添加和/或移动...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
  • 2011-04-30
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
相关资源
最近更新 更多