Javascript 返回选项卡时触发功能

Javascript 返回选项卡时触发功能,javascript,jquery,html,Javascript,Jquery,Html,我正在寻找一个javascript/jQuery方法,以便在返回到选项卡时触发函数。我正在使用getDate on load来跟踪花费在广告上的时间,并在离开广告时减去差异。但是,广告中的一些链接会导致一个新选项卡,用户可能会返回以继续与广告交互。当用户导航回该选项卡时,我可以使用什么来重新启动计时器?Onload似乎不起作用,因为该选项卡已经加载,并且onfocus触发次数太多(用户与广告交互以执行库存的实时搜索)。有什么建议吗?像这样的建议应该可以: $(window).on('blur f

我正在寻找一个javascript/jQuery方法,以便在返回到选项卡时触发函数。我正在使用getDate on load来跟踪花费在广告上的时间,并在离开广告时减去差异。但是,广告中的一些链接会导致一个新选项卡,用户可能会返回以继续与广告交互。当用户导航回该选项卡时,我可以使用什么来重新启动计时器?Onload似乎不起作用,因为该选项卡已经加载,并且onfocus触发次数太多(用户与广告交互以执行库存的实时搜索)。有什么建议吗?

像这样的建议应该可以:

$(window).on('blur focus', function(e) {
  // The window has been focussed, when previously blurred.
  if(e.type == 'focus' && $(this).data('type') == 'blur') {
    alert('You have returned!')
  }
  // Store the event type.
  $(this).data('type', e.type)
})

像这样的事情应该可以做到:

$(window).on('blur focus', function(e) {
  // The window has been focussed, when previously blurred.
  if(e.type == 'focus' && $(this).data('type') == 'blur') {
    alert('You have returned!')
  }
  // Store the event type.
  $(this).data('type', e.type)
})

像这样的事情应该可以做到:

$(window).on('blur focus', function(e) {
  // The window has been focussed, when previously blurred.
  if(e.type == 'focus' && $(this).data('type') == 'blur') {
    alert('You have returned!')
  }
  // Store the event type.
  $(this).data('type', e.type)
})

像这样的事情应该可以做到:

$(window).on('blur focus', function(e) {
  // The window has been focussed, when previously blurred.
  if(e.type == 'focus' && $(this).data('type') == 'blur') {
    alert('You have returned!')
  }
  // Store the event type.
  $(this).data('type', e.type)
})

在返回到选项卡时工作正常。谢谢你的帮助!在返回到选项卡时工作正常。谢谢你的帮助!在返回到选项卡时工作正常。谢谢你的帮助!在返回到选项卡时工作正常。谢谢你的帮助!