Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.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 了解如何使用回调函数_Javascript_Jquery - Fatal编程技术网

Javascript 了解如何使用回调函数

Javascript 了解如何使用回调函数,javascript,jquery,Javascript,Jquery,我对javascript和Jquery还是新手。 我知道javascript会在遇到每一行时执行它。 因此在这种情况下(当我的值>9时) 自定义警报将触发 window.open将立即执行 我希望自定义警报触发,一旦警报关闭,就让window.open执行 我怎样才能做到这一点 if ($('#MyCount').val() > 9) { MyCustom.alert("MyTitle", " Some text.... "); wind

我对javascript和Jquery还是新手。 我知道javascript会在遇到每一行时执行它。 因此在这种情况下(当我的值>9时)

  • 自定义警报将触发
  • window.open将立即执行
我希望自定义警报触发,一旦警报关闭,就让window.open执行

我怎样才能做到这一点

if ($('#MyCount').val() > 9) {
            MyCustom.alert("MyTitle", " Some text.... ");
            window.open(url, 'Print', "toolbar=no,menubar=no,status=no");
}else {
      window.open(url, 'Print', "toolbar=no,menubar=no,status=no");
}
改为

var myCount = parseInt($('#MyCount').val());
if (!isNaN(myCount) && myCount > 9) {
其余部分取决于
MyCustom.alert
的实现。如果它是javascript native
alert
的包装器,那么您的代码将按原样工作。如果它使用的是html对话框,您需要向它传递一个回调,以便在它关闭时运行。

我只是为您准备了这个。。我希望这是有道理的

基本上,我们将回调函数作为第三个参数传递给
MyCustom.alert
,并从jqueryui中侦听
对话框close
事件

HTML
什么是MyCustom.alert?它是否有任何可以附加处理程序的事件?您的自定义警报需要接受一个
函数引用
作为第二个参数,当触发close方法时,该参数将触发。将
自定义
类的警报方法实现更改为接受第三个参数作为回调函数,它将在
alert
function.MyCustom.alert中的行后调用,是一个已编写的内部控件。因此,我需要添加一个新参数,该参数将采用我传入的函数并执行它?是否仅使用本机浏览器
alert
?如果是这样的话,那么我觉得你的代码可以工作(请参阅)。。如果是某个自定义警报,则需要将函数参数传递给警报函数(看起来它将是第三个参数),并在警报完成后执行它。(希望您有听众,或者这是一个承诺)感谢您对javascript的清理。
var myCount = parseInt($('#MyCount').val());
if (!isNaN(myCount) && myCount > 9) {
<input id="MyCount" type="text" />
<input type="button" id="test" />

<div id="someAlert">
    <h1></h1>
    <p></p>
</div>
var MyCustom = {};

MyCustom.alert = function(p1, p2, callback) {
    $("#someAlert").dialog();
    $("#someAlert").children("h1").text(p1);
    $("#someAlert").children("p").text(p2);

    alert(typeof(callback));

    $("#someAlert").on('dialogclose', function(){
        callback();
    });
};

function doSomething(){
 alert("I'm the callback");   
}

var greaterThan9 = function () { 
    if ($('#MyCount').val() > 9) {
                MyCustom.alert("MyTitle", " Some text.... ", doSomething);
                //window.open(url, 'Print', "toolbar=no,menubar=no,status=no");
    } else {
    }
}

$("#test").on("click", function(){
   greaterThan9(); 
});