Javascript按一定顺序执行代码
在assignPopups()中,我需要调用retrievePopupText()来填充mouseOverText变量。运行时,mouseOverText变量在弹出窗口中显示为未定义。弹出窗口和查找功能正常,但在显示弹出窗口之前,我不知道如何填充弹出窗口文本值。有人能告诉我如何构造代码以使其按正确的顺序工作吗?我的查找现在是硬编码的,但当我开始工作时,我会将其更改为适用于许多链接Javascript按一定顺序执行代码,javascript,jquery,Javascript,Jquery,在assignPopups()中,我需要调用retrievePopupText()来填充mouseOverText变量。运行时,mouseOverText变量在弹出窗口中显示为未定义。弹出窗口和查找功能正常,但在显示弹出窗口之前,我不知道如何填充弹出窗口文本值。有人能告诉我如何构造代码以使其按正确的顺序工作吗?我的查找现在是硬编码的,但当我开始工作时,我会将其更改为适用于许多链接 var mouseOverText; function assignPopups(selector) {
var mouseOverText;
function assignPopups(selector) {
$(selector).hover(
function (b) {
if ($(this).attr('title')) {
$(this).data('title', $(this).attr('title'));
$(this).removeAttr('title');
}
if ($(this).data('title')) {
var bkgColor = $(this).closest("td").css("background-color");
rgb2hex(bkgColor);
if (bkgColor === null || bkgColor === undefined) {
bkgColor = "#4aacc5";
}
var Definitions;
retrievePopupText("data/definitions.json", 'LinkID', 'a2');
var html = '<div id="titlePopup" class="tooltip info" style="background-color:' + bkgColor + '; display: block; top: '
+ (b.pageY + 10)
+ 'px; left: '
+ (b.pageX + 20)
+ 'px;">'
+ '<span id="temp">' + mouseOverText + '</span>'
+ '</div>';
timerPopup = setTimeout(function () {
$('div#titlePopup').remove();
$('body').append(html);
}, 800);
}
},
function () {
clearTimeout(timerPopup);
$('div#titlePopup').remove();
});
}
function retrievePopupText(path, key, id) {
var item;
$.getJSON(path)
.done(function (data) {
if (!data) {
return
}
$.each(data.Definitions, function (i, val) {
if (val.LinkID === id) {
mouseOverText = val;
}
})
})
.then(function (data) {
})
.fail(function (e) {
});
}
var mouseOverText;
功能分配弹出窗口(选择器){
$(选择器)。悬停(
职能(b){
if($(this.attr('title')){
$(this.data('title',$(this.attr('title'));
$(this.removeAttr('title');
}
if($(this).data('title')){
var bkgColor=$(this).closest(“td”).css(“背景色”);
rgb2hex(bkgColor);
if(bkgColor==null | | bkgColor==undefined){
bkgColor=“#4aacc5”;
}
var定义;
retrievePopupText(“data/definitions.json”,“LinkID”,“a2”);
var html=''
+''+mouseOverText+''
+ '';
timerPopup=setTimeout(函数(){
$('div#titlepoup')。删除();
$('body').append(html);
}, 800);
}
},
函数(){
清除超时(timerPopup);
$('div#titlepoup')。删除();
});
}
函数retrievePopupText(路径、键、id){
var项目;
$.getJSON(路径)
.完成(功能(数据){
如果(!数据){
返回
}
$.each(data.Definitions,function(i,val){
如果(val.LinkID==id){
mouseOverText=val;
}
})
})
.then(功能(数据){
})
.失败(功能(e){
});
}
您需要使用$.ajax()同步调用它,如下所示:
$.ajax({
url: 'data/definitions.json',
dataType: 'json',
async: false,
success: function(data) {
//stuff
//...
}
});
$.getJSON
是异步的,这意味着它独立于您的程序运行。基本上,您的代码在未加载和准备就绪的情况下需要一些东西
一旦调用进入success函数,则在原始
retrievePopupText
函数下执行所有弹出处理 从已完成回调中调用assignPopups方法:
var mouseOverText;
function assignPopups(selector) {
$(selector).hover(
function (b) {
if ($(this).attr('title')) {
$(this).data('title', $(this).attr('title'));
$(this).removeAttr('title');
}
if ($(this).data('title')) {
var bkgColor = $(this).closest("td").css("background-color");
rgb2hex(bkgColor);
if (bkgColor === null || bkgColor === undefined) {
bkgColor = "#4aacc5";
}
var Definitions;
retrievePopupText("data/definitions.json", 'LinkID', 'a2');
var html = '<div id="titlePopup" class="tooltip info" style="background-color:' + bkgColor + '; display: block; top: '
+ (b.pageY + 10)
+ 'px; left: '
+ (b.pageX + 20)
+ 'px;">'
+ '<span id="temp">' + mouseOverText + '</span>'
+ '</div>';
timerPopup = setTimeout(function () {
$('div#titlePopup').remove();
$('body').append(html);
}, 800);
}
},
function () {
clearTimeout(timerPopup);
$('div#titlePopup').remove();
});
}
function retrievePopupText(path, key, id) {
var item;
$.getJSON(path)
.done(function (data) {
if (!data) {
return
}
$.each(data.Definitions, function (i, val) {
if (val.LinkID === id) {
mouseOverText = val;
}
})
assignPopups('.your-selector'); // e.g. here
})
.then(function (data) {
})
.fail(function (e) {
});
}
var mouseOverText;
功能分配弹出窗口(选择器){
$(选择器)。悬停(
职能(b){
if($(this.attr('title')){
$(this.data('title',$(this.attr('title'));
$(this.removeAttr('title');
}
if($(this).data('title')){
var bkgColor=$(this).closest(“td”).css(“背景色”);
rgb2hex(bkgColor);
if(bkgColor==null | | bkgColor==undefined){
bkgColor=“#4aacc5”;
}
var定义;
retrievePopupText(“data/definitions.json”,“LinkID”,“a2”);
var html=''
+''+mouseOverText+''
+ '';
timerPopup=setTimeout(函数(){
$('div#titlepoup')。删除();
$('body').append(html);
}, 800);
}
},
函数(){
清除超时(timerPopup);
$('div#titlepoup')。删除();
});
}
函数retrievePopupText(路径、键、id){
var项目;
$.getJSON(路径)
.完成(功能(数据){
如果(!数据){
返回
}
$.each(data.Definitions,function(i,val){
如果(val.LinkID==id){
mouseOverText=val;
}
})
分配弹出窗口('.your selector');//例如,此处
})
.then(功能(数据){
})
.失败(功能(e){
});
}
啊,是的……我一直忘了异步:false。谢谢