所以我有这两组 JavaScript 函数,它们不断地连接我的服务器,如果数据库已经更新,这些函数会做一些事情。两组功能彼此无关;它们唯一的关系是它们同时发生。然而,由于某种原因,一个功能与另一个功能发生冲突。这是我的代码的样子:
<!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>
我不想详细介绍这些函数集的作用(除非要求),因为我不希望这篇文章真的很长。但问题是这两组函数本身都可以正常工作,但由于某种原因,如果你同时运行它们,第二个函数组一直说当 state 等于 0 时没有定义 popupwindow。
这没有任何意义!为什么第一个函数集会导致这种情况发生?:(