简体   繁体   English

如何使用变量而不是名称来调用函数?

[英]How can I call a function by using a variable instead of its name?

Here is my code: 这是我的代码:

 $('.click').on('click', function(){ $.ajax({ url : $(this).siblings('a').attr('href'), dataType : 'JSON', success : function (tags) { $("ul").html(tags.output); }, error : function (jqXHR, textStatus, errorThrown) { if ( textStatus === 'timeout') { ajax_error_handleign('timeout error', 10, 5000, 'clear_result_box'); } }, timeout: 4000 }); }) function ajax_error_handleign(message, position, aliev_time = 5000, func_name = false){ if ( func_name !== false ){ window['clear_result_box'](); } $(".error_msg").html(message); $(".error").css({top: position + "px"}).fadeIn(200); close_error_msg = setTimeout(function(){ $(".error").fadeOut(100); }, aliev_time); } function clear_result_box () { console.log('result box is clear now') } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class='click'> click </button> 

Also I have sleep(5); 我也sleep(5); in the php script that ajax request above refers to. 在上面的ajax请求所引用的php脚本中。 So always timeout reaches and error will be executed. 因此,始终会timeout并且将执行error

What's the problem? 有什么问题? I get this error: 我收到此错误:

在此处输入图片说明

Does anybody what's wrong? 有人出什么事了吗?

instead of: 代替:

function clear_result_box () {
    console.log('result box is clear now')
}

assign the function to a variable: 将函数分配给变量:

var clear_result_box = function(){
    console.log('result box is clear now')
}

and use the variable after: 并在之后使用变量:

ajax_error_handleign('timeout error', 10, 5000, clear_result_box);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM