代码不按顺序执行 [英] Code not executing sequentially

查看:105
本文介绍了代码不按顺序执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码似乎没有按照它编写的顺序执行,我不知道为什么...在我的代码中我从xml文件读取一些单词然后将其推入数组(我使用此数组为autocomplete)...我的代码是:



var orig = CodeMirror.hint.anyword;

var inner;

var txt =;



CodeMirror.hint.anyword = function(cm){

inner = orig(cm) || {from:cm.getCursor(),to:cm.getCursor()};

if(window.yourGlobalVariable === 0)

{

//////从xml文件中读取单词(Employee)///////

var xmlhttp = new XMLHttpRequest();

xmlhttp .onreadystatechange = function(){

if(this.readyState == 4&& this.status == 200){

myFunction(this);

}

};

xmlhttp.open(GET,符号表Example.xml,true);

xmlhttp.send();





函数myFunction(xml){

var x,i ,xmlDoc;

xmlDoc = xml.responseXML;



x = xmlDoc.getElementsByTagName(TemplateDef);

y = xmlDoc.getElementsByTagName(TemplateField);



for(i = 0; i< x.length; i ++){

如果(X [I] .getAttribute( '的className')!= = null)

{

window.txt = x [i] .getAttribute('className');

}



}



}



inner.list.length = 0;



inner.list.push(class1);

inner.list.push(println); < br $>
inner.list.push(program);

///////推送列表中的单词////////

inner.list.push(window.txt);

window.ClassName.length = 0;

window.ClassName = inner.list.slice() ;

返回内部;

}



当我第一次按(ctrl_space)然后单词不添加到列表....第一次我自动完成代码后执行正确...



所以我不知道为什么我第一次执行代码不按顺序执行??提前谢谢



我尝试过:



我的代码是以上可以任何正文帮助

解决方案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

I have some code that does not seem to execute in the order it is written , I did not knew why ... In my code I read some word from xml file and then push it into array (I use this array for autocomplete) ... my code is :

var orig = CodeMirror.hint.anyword;
var inner;
var txt = " ";

CodeMirror.hint.anyword = function(cm) {
inner = orig(cm) || { from: cm.getCursor(), to: cm.getCursor()};
if(window.yourGlobalVariable===0)
{
////// read the word (Employee) from xml file ///////
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xmlhttp.open("GET", "Symbol Table Example.xml", true);
xmlhttp.send();


function myFunction(xml) {
var x, i, xmlDoc;
xmlDoc = xml.responseXML;

x = xmlDoc.getElementsByTagName("TemplateDef");
y=xmlDoc.getElementsByTagName("TemplateField");

for (i = 0; i< x.length; i++) {
if(x[i].getAttribute('className')!==null)
{
window.txt = x[i].getAttribute('className') ;
}

}

}

inner.list.length = 0;

inner.list.push("class1");
inner.list.push("println");
inner.list.push("program");
/////// push the word in list ////////
inner.list.push(window.txt);
window.ClassName.length = 0;
window.ClassName=inner.list.slice();
return inner;
}

when I press (ctrl_space) to the first time then the word not add to list .... after first time I do autocomplete the code execute correct ...

so I did not knew why in first time I execute the code not executing sequentially ?? thanks in advance

What I have tried:

My code is above can any body help

解决方案

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.


这篇关于代码不按顺序执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆