通过document.hidden属性判断当前页面是否是激活状态

网页当前状态判断,若不处于当前窗口则不发请求

var interval_output = null;
function sendRequst() {
     interval_output = setInterval(function () {
        SetClock();
    }, 60000);
}
sendRequst();

var hiddenProperty = 'hidden' in document ? 'hidden' :
'webkitHidden' in document ? 'webkitHidden' :
'mozHidden' in document ? 'mozHidden' :null;


var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
var onVisibilityChange = function () {
if (!document[hiddenProperty]) {
     // console.log('页面激活')
        sendRequst();
   } else {
      //console.log('页面非激活');
       clearInterval(interval_output);
  }
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);

转载于:https://www.cnblogs.com/James123/p/10168848.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python内置的`os`模块来判断当前目录下是否存在指定文件。以下是一个示例代码: ```python import os if os.path.isfile('li.json'): print('li.json文件存在') else: print('li.json文件不存在') if os.path.isfile('document.txt'): print('document.txt文件存在') else: print('document.txt文件不存在') ``` 在这个示例代码中,`os.path.isfile()`函数用于判断当前目录下是否存在指定文件。如果文件存在,则返回`True`;否则返回`False`。 ### 回答2: 在Python中,可以通过使用os模块来判断当前目录下是否存在特定文件。对于给定的文件路径,可以使用os.path.exists()函数来检查该文件是否存在。以下是判断当前目录下是否存在"li.json"文件和"document.txt"文件的代码示例: ```python import os # 获取当前目录路径 current_dir = os.getcwd() # 判断li.json文件是否存在 li_json_path = os.path.join(current_dir, "li.json") if os.path.exists(li_json_path): print("li.json文件存在") else: print("li.json文件不存在") # 判断document.txt文件是否存在 document_txt_path = os.path.join(current_dir, "document.txt") if os.path.exists(document_txt_path): print("document.txt文件存在") else: print("document.txt文件不存在") ``` 以上代码首先获取当前目录路径,然后使用os.path.join()函数将文件名和目录路径合并为完整的文件路径。接着利用os.path.exists()函数分别检查"li.json"文件和"document.txt"文件是否存在。根据判断结果,输出相应的提示信息。 希望以上回答能够帮助到您! ### 回答3: 可以使用Python的os模块和os.path模块来判断当前目录下是否存在特定文件。 首先,我们需要导入os模块和os.path模块: ```python import os ``` 然后,我们可以使用os.path.exists()函数来判断文件是否存在。这个函数接受文件路径作为参数,并返回布尔值,True表示文件存在,False表示文件不存在。 ```python json_file_exists = os.path.exists("li.json") txt_file_exists = os.path.exists("document.txt") ``` 接下来,我们可以根据返回的布尔值进行判断,并输出相应的结果。 ```python if json_file_exists: print("li.json文件存在") else: print("li.json文件不存在") if txt_file_exists: print("document.txt文件存在") else: print("document.txt文件不存在") ``` 最后,我们可以将以上代码整合在一起,并进行测试: ```python import os json_file_exists = os.path.exists("li.json") txt_file_exists = os.path.exists("document.txt") if json_file_exists: print("li.json文件存在") else: print("li.json文件不存在") if txt_file_exists: print("document.txt文件存在") else: print("document.txt文件不存在") ``` 运行以上代码,即可判断当前目录下是否存在li.json文件和document.txt文件,并输出相应的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值