来自ajax和getjson响应的数据在页面的其余部分不可用;s javascript
我遇到了一个问题,我毫无疑问怀疑我在AJAX和JSON方面的新手技能。我的设置是这样的:我有一个wallboard脚本对象,它在HTML的末尾被调用。在wallboard被实例化之后,我有脚本标记(不是挂在HTML末尾的这些标记的粉丝)来创建BXSlider对象。我的wallboard对象和bxslider对象都有内部ajax调用,其中一个是getJSON。我的问题是:毫不奇怪,我的异步调用没有及时返回响应,页面无法接受数据。当然,如果我发出警报(“测试”);它为Firefox 12提供了足够的加载时间,但在Safari 5.1或IE 9中仍然不起作用 HTML:来自ajax和getjson响应的数据在页面的其余部分不可用;s javascript,ajax,document-ready,bxslider,Ajax,Document Ready,Bxslider,我遇到了一个问题,我毫无疑问怀疑我在AJAX和JSON方面的新手技能。我的设置是这样的:我有一个wallboard脚本对象,它在HTML的末尾被调用。在wallboard被实例化之后,我有脚本标记(不是挂在HTML末尾的这些标记的粉丝)来创建BXSlider对象。我的wallboard对象和bxslider对象都有内部ajax调用,其中一个是getJSON。我的问题是:毫不奇怪,我的异步调用没有及时返回响应,页面无法接受数据。当然,如果我发出警报(“测试”);它为Firefox 12提供了足够的
墙板
身体{
溢出-x:自动;
溢出y:自动;
}
身体,h1{
保证金:0;
}
#容器{
显示:内联;
}
#左面板{
位置:固定;
宽度:350px;
背景色:#E0DEDE;
右边框:1px纯色灰色;
左边框:1px纯色灰色;
}
#右面板{
位置:相对位置;
左边距:352px;
背景色:#f2efeff;
左边框:1px实心#FFF;
}
#右面板状态{
位置:绝对位置;
底部:0px;
宽度:100%;
背景颜色:灰色;
边框顶部:1px实心暗光;
}
#rightPanelStatus.cmdBar{
文本对齐:居中;
}
#slideContainer{
列表样式:无;
}
李{
文本对齐:居中;
}
img{
高度:169px;
宽度:169px;
}
.slideControl{
显示:内联;
光标:指针;
}
#播放控制,暂停控制{
利润率:10px;
填充:0 5px 0 5px;
}
暂停播放
/*缩放容器高度以调整浏览器窗口大小。IE 7讨厌这个*/
函数changehight(){
var sh=460;
if(document.body&&document.body.offsetWidth){
sh=document.body.offsetHeight;
}
如果(document.compatMode=='CSS1Compat'&&
document.documentElement&&
document.documentElement.offsetWidth){
sh=document.documentElement.offsetHeight;
}
if(window.innerWidth和window.innerHeight){
sh=窗内高度;
}
document.getElementById(“容器”).style.height=sh+“px”;
document.getElementById(“leftPanel”).style.height=sh+“px”;
document.getElementById(“rightPanel”).style.height=sh+“px”;
}
/*加载页面后获取浏览器屏幕大小的初始调用*/
改变高度();
/*墙板对象*/
myWallboard=新墙板();
myWallboard.loadSlides();
警报(“测试”);//这是上述警报测试
$(文档).ready(函数(){
/*滑块对象*/
变量滑块=$('#slideContainer')。bxSlider({
是的,
自动控制:错误,
模式:“垂直”,
控件:false,
速度:1000,
显示幻灯片数量:5,
移动滑块数量:1,
onBeforeSlide:函数(当前幻灯片){
myWallboard.setSlideSpeed(滑块、currentSlide、myWallboard.slides);
myWallboard.AJAXRequest(myWallboard.slides[currentSlide][1],“rightPanelContainer”);
}
});
var cs=slider.getCurrentSlide();
myWallboard.setSlideSpeed(滑块、cs、myWallboard.slides);
myWallboard.AJAXRequest(myWallboard.slides[cs][1],“rightPanelContainer”);
/*控制*/
$('#pauseControl')。单击(函数(){
document.getElementById('pauseControl').style.backgroundColor=“#C4”
document.getElementById('pauseControl').style.padding=“4px”
document.getElementById('playControl').style.backgroundColor=“”
document.getElementById('playControl').style.padding=“0 4px 0 4px”
myWallboard.stopSlideTimer();
slider.stopShow();
返回false;
});
$('#playControl')。单击(函数(){
document.getElementById('playControl').style.backgroundColor=“#C4C4”
document.getElementById('playControl').style.padding=“4px”
document.getElementById('pauseControl').style.backgroundColor=“”
document.getElementById('pauseControl').style.padding=“0 4px 0 4px”
slider.startShow();
返回false;
});
});
wallboard.js
/* wallboard constructor */
function wallboard() {
var t = 0;
var _this = this;
this.slides = new Array(5);
/* create a multidimensional array of each slide. */
for (var i = 0; i < this.slides.length; i++) {
/* create next dimension (url) */
this.slides[i] = new Array(3);
}
this.loadSlides = function () {
var _this = this;
$.getJSON('jsonReturn.aspx', function (json, status) {
var i = 0;
$.each(json, function (key, val) {
_this.slides[i][0] = val.imagePath;
_this.slides[i][1] = val.url;
_this.slides[i][2] = val.duration;
i++;
});
for (var i = 0; i < _this.slides.length; i++) {
document.getElementById('slideContainer').innerHTML += '<li><img src="' + _this.slides[i][0] + '" /></li>\n';
}
});
}
this.startSlide = function (slider) {
slider.startShow();
}
this.stopSlideTimer = function () {
try {
// alert("pausing slides. killing setTimeout " + t);
clearTimeout(t);
return false;
}
catch (err) {
alert("Could not stop slide timer.");
}
}
this.setSlideSpeed = function (slider, cs, sa) {
var _this = this;
clearTimeout(t);
slider.stopShow();
//alert('testing sa[cs][2]: ' + sa[cs][2]);
t = setTimeout(function () { _this.startSlide(slider); }, sa[cs][2] * 1000);
}
this.AJAXRequest = function (uri, div) { // leftover YUI, replace with jQuery
// Create a YUI instance using io-base module.
YUI().use("io-base",
function (Y) {
/* the following can be used to post variables to a page this
may be useful for sending parameters to a server side page */
var cfg = {
method: 'POST',
form: {
id: 'myform',
useDisabled: false
}
};
{
// Define a function to handle the Ajax response data.
function complete(id, o, args) {
// used for debugging
//alert("id:" + id + ", o.responseText: " + o.responseText + ", args: " + args);
// put the html returned in the main slide div
document.getElementById(div).innerHTML = o.responseText;
};
// Subscribe to event "io:complete", and pass an array
// as an argument to the event handler "complete"
Y.on('io:complete', complete, Y, ['arguments (these may not be needed)']);
var request = Y.io(uri);
}
});
}
}
/*墙板建造师*/
功能墙板(){
var t=0;
var_this=这个;
this.slides=新数组(5);
/*为每张幻灯片创建多维数组*/
对于(var i=0;i\n';
}
});
}
this.startSlide=函数(滑块){
slider.startShow();
}
this.stopSlideTimer=函数(){
试一试{
//警报(“暂停幻灯片。终止设置超时”+t);
清除超时(t);
返回false;
}
捕捉(错误){
警报(“无法停止滑动计时器”);
}
}
this.setSlideSpeed=功能(滑块、cs、sa){
var_this=这个;
清除超时(t);
slider.stopShow();
//警报(“测试sa[cs][2]:”+sa[cs][2]);
t=设置超时(函数
/* wallboard constructor */
function wallboard() {
var t = 0;
var _this = this;
this.slides = new Array(5);
/* create a multidimensional array of each slide. */
for (var i = 0; i < this.slides.length; i++) {
/* create next dimension (url) */
this.slides[i] = new Array(3);
}
this.loadSlides = function () {
var _this = this;
$.getJSON('jsonReturn.aspx', function (json, status) {
var i = 0;
$.each(json, function (key, val) {
_this.slides[i][0] = val.imagePath;
_this.slides[i][1] = val.url;
_this.slides[i][2] = val.duration;
i++;
});
for (var i = 0; i < _this.slides.length; i++) {
document.getElementById('slideContainer').innerHTML += '<li><img src="' + _this.slides[i][0] + '" /></li>\n';
}
});
}
this.startSlide = function (slider) {
slider.startShow();
}
this.stopSlideTimer = function () {
try {
// alert("pausing slides. killing setTimeout " + t);
clearTimeout(t);
return false;
}
catch (err) {
alert("Could not stop slide timer.");
}
}
this.setSlideSpeed = function (slider, cs, sa) {
var _this = this;
clearTimeout(t);
slider.stopShow();
//alert('testing sa[cs][2]: ' + sa[cs][2]);
t = setTimeout(function () { _this.startSlide(slider); }, sa[cs][2] * 1000);
}
this.AJAXRequest = function (uri, div) { // leftover YUI, replace with jQuery
// Create a YUI instance using io-base module.
YUI().use("io-base",
function (Y) {
/* the following can be used to post variables to a page this
may be useful for sending parameters to a server side page */
var cfg = {
method: 'POST',
form: {
id: 'myform',
useDisabled: false
}
};
{
// Define a function to handle the Ajax response data.
function complete(id, o, args) {
// used for debugging
//alert("id:" + id + ", o.responseText: " + o.responseText + ", args: " + args);
// put the html returned in the main slide div
document.getElementById(div).innerHTML = o.responseText;
};
// Subscribe to event "io:complete", and pass an array
// as an argument to the event handler "complete"
Y.on('io:complete', complete, Y, ['arguments (these may not be needed)']);
var request = Y.io(uri);
}
});
}
}