0

so I've got these two sets of JavaScript functions that continuously pole my server and, if the database has been updated, the functions do something. Both sets of functions have nothing to do with each other; they're only relationship is that they happen at the same time. Yet for some reason one function is conflicting with the other. Here's what my code looks like:

<!DOCTYPE html>
<html>
<head>
<title>Stage</title>
<link href="stage.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript">

 ///////////////////// FUNCTION SET 1 

function loadThumbnail(){
  $.ajax({
    url: 'process_stage_show.php',
    data: 'value=<?php echo $_GET["session"]; ?>',       
    dataType: 'json',                           
    success: function(data){
      var idCurrent = data[0];
      var idVideo = data[1];
      var idSession = data[2];
      var state = data[4];
      if (state ==  1) {
        $('#tv').html('<img src="http://i.ytimg.com/vi/'+ idVideo +'/hqdefault.jpg" width ="800" height="533"/>');
        setTimeout(function(){timedCount2(idCurrent);},1000);
      }
      else {
        setTimeout(function(){timedCount2(idCurrent);},1000);
      }
    }
  });
}

function timedCount2(idCurrent){
  $.ajax({                                      
    url: 'process_stage_show.php',                
    data: 'value=<?php echo $_GET["session"]; ?>',       
    dataType: 'json',
    success: function(data) {  
      var idNew = data[0];  
      var idVideo = data[1];
      var idSession = data[2];          
      var state = data[4];          
      if (!(idCurrent == idNew)) {
        window.location.reload();
      }
      else{
        setTimeout(function(){timedCount2(idCurrent);},1000);
      }
    } 
  });
}

 ///////////////////// FUNCTION SET 2 

function loadStage(){
  $.ajax({
    url: 'process_stage_play.php',            
    data: 'value=<?php echo $_GET["session"]; ?>',       
    dataType: 'json',                               
    success: function(data){                    
      var idVideo = data[0];        
      var idCurrent = data[1];
      var idSession = data[2];
      var state = data[4];
      if (state ==  2) {
        //alert("State equal to 2. Do nothing.");
        setTimeout(function(){timedCount(idCurrent);},1000);
      }
      else if (state == 1) {
        //alert("State equal to 1. Open window.");
        popupwindow = window.open ("http://www.youtube.com/watch_popup?v="+ idVideo);
        setTimeout(function(){timedCount(idCurrent);},1000);
      }
      else if (state == 0) {
        //alert("State equal to 0. Close window.");
        popupwindow.close();
        setTimeout(function(){timedCount(idCurrent);},1000);
      }
    }
  });
}

function timedCount(idCurrent){
  $.ajax({                                      
    url: 'process_stage_play.php'             
    data: 'value=<?php echo $_GET["session"]; ?>',
    dataType: 'json',                               
    success: function(data) {  
      var idVideo = data[0];  
      var idNew = data[1];
      var idSession = data[2];          
      var state = data[4];          
      if (idCurrent == idNew) {
        setTimeout(function(){timedCount(idCurrent);},1000);
      }
      else{
        //alert("ID has changed. Re-load stage.");
        loadStage();
      }
    } 
  });
}

</script>
</head>

<body onload="loadStage(); loadThumbnail();">

<div id="tv"></div>

</body>
</html>

I'm don't want to go into too much detail about what these sets of functions do (unless requested) as I don't want this post to be REALLY long. But the problem is both these sets of functions work fine by themselves but for some reason if you run them at the same time, the second function set keeps saying popupwindow is not defined when state is equal to 0.

Which makes no sense! Why would the first function set cause that to happen? :(

4

2 に答える 2

1

Well one of the things that the first function sometimes does is reload the window. As soon as that happens, the global variable "popupwindow" will vanish.

It might work to have code in the popped-up window check its "opener" window periodically to see whether it should close itself.

于 2012-08-20T18:51:04.630 に答える
1

Do I understand correctly that these functions both open a popup window? If so, some browsers do only open one popup per webpage and if another popup is opened, the old one will be refreshed with the new url.

Thus, one of these functions could lose "its" popup window which would explain your problem.

于 2012-08-20T18:51:51.377 に答える