I have an array that gets updated together with the rest of the page via an ajax call that replaces the ent开发者_运维问答ire body contents of my page. My <script>
tags are inside the body, and I explicitly run eval()
on those script tags (referenced with id).
Now, if I do an alert(array)
I get the updated values, but the functions in my Javascript that make use of the contents of my array never updates, it hangs on to the old array values inspite of the update.
This is the eval()
that should do the trick...
eval(document.getElementById('otherScripts').innerHTML);
eval(document.getElementById('transition').innerHTML);
<?php
javascript.="pauseTimes[$i]=$paustime"
?>
That ends up something like this Javascript array:
pausTimes[0]=3000
pausTimes[1]=3000
pausTimes[2]=3000
pausTimes[3]=3000
Like I said, when doing an alert()
on this array's contents, it displays the correct, updated values, but this little function below just doesn't update. It is as if it drew these array values from a cache of it's own:
function slideshow(slide) {
if (pauseTimes[slide]>0) {
$('#slide'+slide).fadeIn(1500).delay(pauseTimes[slide])**.fadeOut(1500,function(){slideshow(slide+1);});
}
else {
ajaxUpdate();
}
}
The critical part above is the one in bold, here I constantly get the old, initial array values from the first, synchronous page load. After that, no matter what the contents of the array pauseTimes
, is stays the same.
Depends on what the script passed to your eval() looks like.
You may be setting those values to the wrong pauseTimes
variable object, or an object located in the wrong place. That's why you are not overwriting the values in the object that you really care.
However, without a sample of your script, it is impossible to tell.
I found the bug myself - when declaring the array pauseTimes I used the keyword var which makes the variable local to that script block.
Erasing that keyword solved the problem, my array is now global to the window and will update as intended.
/Mattias
Do I have to in some way unset the array pauseTimes and declare it anew? You said something about setting another array object, is it then possible to have two arrays with the same name?
Here is the sampe code. Like I said, it works perfectly except that the times aren't updated, contents of my div's and such update as it should.
<script id='transition' type='text/javascript' src='userData/2/style/transition.js'></script>
<script id='otherScripts' type='text/javascript'>
var pauseTimes = new Array();
setPauseTimes();
function setPauseTimes() {
pauseTimes[0]=2000;
pauseTimes[1]=2000;
pauseTimes[2]=2000;
pauseTimes[3]=2000;
}
function ajaxUpdate()
{
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function() {
/* Försök hämta hela bildspelet på nytt */
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
/* Ladda in bildspelet på nytt */
document.getElementById('slideshow_body').innerHTML=xmlhttp.responseText;
/* Uppdatera paustiderna */
eval(document.getElementById('otherScripts').innerHTML);
eval(document.getElementById('transition').innerHTML);
}
/* Om hämtningen misslyckades... */
else if (xmlhttp.readyState==4 && xmlhttp.status!=200)
{
/* Visa avbrottsmeddelandet */
document.getElementById('avbrott').style.display='inline';
/* Starta om bildspelet från utgångsläget (det som redan finns inläst) */
slideshow(0);
}
}
xmlhttp.open('GET','index.php?controller=slideshow&asynch=true',true);
xmlhttp.send();
}
function buttons(id) {
$('#button'+id).removeClass('future').addClass('past');
}
function buttons_reset() {
/* Ställa om alla buttons till future (icke ifyllda) */
$('#buttons').find('div').each(function(i){
$('#button'+i).removeClass('past').addClass('future');
});
}
$(document).ready(function(){
slideshow(0);
});
</script>
Contents of the transition.js linked script:
function slideshow(slide) { if (slide==0) { buttons_reset();
/*
$('#wrapper').find('div').each(function(i){
$('#slide'+i).fadeOut(0);
});
*/
}
if (pauseTimes[slide]>0) {
buttons(slide);
$('#slide'+slide).fadeIn(1500).delay(pauseTimes[slide]).fadeOut(1500,function(){slideshow(slide+1);});
}
else {
ajaxUpdate();
}
}
精彩评论