Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery无法设置变量_Javascript_Jquery_Function_Variables - Fatal编程技术网

Javascript jquery无法设置变量

Javascript jquery无法设置变量,javascript,jquery,function,variables,Javascript,Jquery,Function,Variables,我有一个代码,在从数据库获取信息后,它应该将变量newcolorid设置为它从数据库获取的id。如您所见,我在函数之前定义了变量,但在设置了新id之后,它仍然保持不变。基本上在控制台中,我看到了新的id,但在函数完成后,它将其恢复为0。有什么问题吗 var newcolorid = 0; $.ajax({ url: "/colors", dataType: "json", success: function(data) { var newcolors =

我有一个代码,在从数据库获取信息后,它应该将变量newcolorid设置为它从数据库获取的id。如您所见,我在函数之前定义了变量,但在设置了新id之后,它仍然保持不变。基本上在控制台中,我看到了新的id,但在函数完成后,它将其恢复为0。有什么问题吗

var newcolorid = 0;

$.ajax({
    url: "/colors",
    dataType: "json",
    success: function(data) {
        var newcolors = [];
        jQuery.each(data.colors, function (i, elem){
            newcolors.push([elem.color_code, elem.label, elem.id]);
        });
        for(var i=0;i<newcolors.length;i++){
            if(newcolors[i][1]==label){
                newcolorid = newcolors[i][2];
                console.log(newcolorid);
                break;
            }
        }
    }
});

for (var i = 0; i < spectrums.length; i++) {
    if (spectrums[i]) {
        var spectrumPalette = spectrums[i].option("palette");
        spectrumPalette.push([[rgb, label, newcolorid]]);
        spectrums[i].option("palette", spectrumPalette);
    }
}
var newcolorid=0;
$.ajax({
url:“/colors”,
数据类型:“json”,
成功:功能(数据){
var newcolors=[];
jQuery.each(data.colors,function(i,elem){
newcolors.push([elem.color_代码、elem.label、elem.id]);
});

对于(var i=0;i来说,这是因为在后端返回值之前执行循环,所以当它运行时,值保持不变

后端调用是异步的(除非另有设置),这意味着它们不会阻止脚本其余部分的执行,而是在收到后端应答时执行

如果要使用新值或将代码移动到从成功回调调用的函数中,则需要将代码移动到成功回调中

var newcolorid = 0;

$.ajax({
    url: "/colors",
    dataType: "json",
    success: function(data) {

        var newcolors = [];

        jQuery.each(data.colors, function (i, elem){
            newcolors.push([elem.color_code, elem.label, elem.id]);
        });

        for(var i = 0; i<newcolors.length; i++){
            if(newcolors[i][1]==label){
                newcolorid = newcolors[i][2];
                console.log(newcolorid);
                break;
            }
        }

        for (i = 0; i < spectrums.length; i++) {
            if (spectrums[i]) {
                var spectrumPalette = spectrums[i].option("palette");
                spectrumPalette.push([[rgb, label, newcolorid]]);
                spectrums[i].option("palette", spectrumPalette);
            }
        }
    }
});
var newcolorid=0;
$.ajax({
url:“/colors”,
数据类型:“json”,
成功:功能(数据){
var newcolors=[];
jQuery.each(data.colors,function(i,elem){
newcolors.push([elem.color_代码、elem.label、elem.id]);
});

对于(var i=0;iajax是异步的,您正在请求完成之前运行代码。请尝试将其移动到回调中。您确定这不是打字错误吗?代码似乎很好。除非您希望在底部循环中有新值。谢谢,这很有效,但有一点延迟,有没有办法使其更快?延迟是由所花的时间造成的s服务器响应,使其更快获得更快的internet连接。显示正在加载的gif是正常的想法,我如何显示正在加载的gif?