如何在JavaScript中实现以下内容?
- Wait 120 seconds
- Open http://192.168.1.1/internet-disconnect (in an iframe or so, doesn't matter)
- Wait 3 seconds
- Open http://192.168.1.1/internet-connect
- Repeat
以下是我的尝试方式:
<meta http-equiv="refresh" content="30">
<iframe id="iframe_id" src="http://heise.de"></iframe>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('iframe#iframe_id').attr('src', 'http://192.168.1.1/internet-disconnect');
setTimeout(function(){
$('iframe#iframe_id').attr('src', 'http://192.168.1.1/internet-connect');
}, 3000);
});
</script>
但它很乱,并且不会按照应有的方式工作。
谢谢!
答案 0 :(得分:0)
如果setTimeout()
,请使用setInterval()试试这个
<script>
$(document).ready(function() {
var flag = 0;
$(document).ready(function() {
setInterval(function(){
if(flag == 0){
$('iframe#iframe_id').attr('src', 'http://192.168.1.1/internet-disconnect');
flag=1;
}else{
$('iframe#iframe_id').attr('src', 'http://192.168.1.1/internet-connect');
flag=0;
}
}, 3000);
});
</script>
答案 1 :(得分:0)
window.onload = function(){
var url1 = 'http://192.168.1.1/internet-disconnect';
var url2 = 'http://192.168.1.1/internet-connect';
var delay1 = 120000;
var delay2 = 3000;
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
setInterval(function(){
setTimeout(function(){iframe.src= url1;},delay1);
setTimeout(function(){iframe.src= url2;},delay2);
},delay1+delay2)
}