在javascript中延迟函数调用 [英] Delaying function call in javascript

查看:66
本文介绍了在javascript中延迟函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好CP,

我目前在Foundation和AlertifyJS工作。下面的函数检查表单元素内容是否等于'天堂'和'地狱'。如果为false,则显示自定义错误消息。但如果成功,它应该显示成功警报,但页面刷新。我正在考虑使用callback()延迟函数返回,但没有什么区别。这是代码。提前致谢。



 $(function(){
$( ' 。run')。click(function( event ){
< span class =code-keyword> var userid = document.getElementById(' uid')。 value ;
var password = document.getElementById(' pass')。 value ;
if (userid == ' heaven'&& ; password == ' hell'){
alertify.success( 成功 );
return true ;
}
else {
alertify.error( 失败);
return false ;
}
});
});





 <  表格   方法  =  post    onsubmit   =  return test();    action   =  success.php >  
< label for = uid > < b > 用户名< / b > < /标签 >
< 输入 类型 = text id = uid 名称 = uid / >
< label for = 传递 > < b > 密码< < span class =code-leadattribute> / b > < / label >
< < span class =code-leadattribute> input type = 密码 id = 传递 名称 = pass / >
< 输入 type = 提交 class = 运行 value = 登录 / >
< / form >

解决方案

(function(){


' 。run')。click(函数( event ){
var userid = document.getElementById(< span class =code-string>' uid')。 value ;
var password = document.getElementById(' 传递')。 value ;
if (userid == ' heaven' && password == ' hell'){
alertify。成功( 成功);
return true ;
}
else {
alertify.error( 失败);
return false ;
}
});
});





 <  表格   方法  =  post    onsubmit   =  return test();    action   =  success.php >  
< label for = uid > < b > 用户名< / b > < /标签 >
< 输入 类型 = text id = uid 名称 = uid / >
< label for = 传递 > < b > 密码< < span class =code-leadattribute> / b > < / label >
< < span class =code-leadattribute> input type = 密码 id = 传递 名称 = pass / >
< 输入 type = 提交 class = 运行 value = 登录 / >
< / form >


< blockquote>除了什么你已经共享,只有当结果(表达式)为真时,页面才会刷新。那是因为你没有停止代码做它想要做的事情,提交表单。



要解决此问题,请将此函数写入代码。 event.preventDefault()将确保页面未刷新,返回false 在其他块中执行此操作您并确保代码不传播并且未提交表单(间接页面不刷新)。



 


Hi CPs,
I am currently working in Foundation and AlertifyJS. Below function checks if form element content is equal to 'heaven' and 'hell'. If it is false it shows the custom error message. But if it succeeds it should show the Success alert but the page refreshes. I was thinking about delaying the function return with callback() but didnt make a difference. Here's the code. Thanks in Advance.

$(function() {
            $('.run').click(function(event) {
                var userid = document.getElementById('uid').value;
                var password = document.getElementById('pass').value;
                if(userid == 'heaven' && password == 'hell') {
                        alertify.success("Success");
                        return true;
                }
                else {
                    alertify.error("Failed");
                    return false;
                }
            });
        });



<form method="post" onsubmit="return test();" action="success.php">
   <label for="uid"><b>Username</b></label>
   <input type="text" id="uid" name="uid"/>
   <label for="pass"><b>Password</b></label>
   <input type="password" id="pass" name="pass"/>
   <input type="submit" class="run" value="Log In"/>
</form>

解决方案

(function() {


('.run').click(function(event) { var userid = document.getElementById('uid').value; var password = document.getElementById('pass').value; if(userid == 'heaven' && password == 'hell') { alertify.success("Success"); return true; } else { alertify.error("Failed"); return false; } }); });



<form method="post" onsubmit="return test();" action="success.php">
   <label for="uid"><b>Username</b></label>
   <input type="text" id="uid" name="uid"/>
   <label for="pass"><b>Password</b></label>
   <input type="password" id="pass" name="pass"/>
   <input type="submit" class="run" value="Log In"/>
</form>


Apart from what you have shared, the page only refreshes in case of when the result (of expression) is true. That is because you have not stopped the code from doing what it is intended to do, submit the form.

To overcome this, write this function to your code. event.preventDefault() would make sure that the page is not refreshed, return false in the other blocks does this for you and makes sure that the code doesn't propagate and form isn't submitted (indirectly page doesn't refresh).


这篇关于在javascript中延迟函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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