javascript - setTimeout 不起作用,尽管可以在另一个类似的脚本中使用

标签 javascript jquery

在 div 中插入一些文本后,我使用不起作用的重定向调用 setTimeout() ,我使用的脚本与我在另一个项目中使用的几乎相同的脚本确实有效,我不明白为什么?

有效的脚本

$.getJSON("http://newberylodge.co.uk/webapp/includes/login1.php",{username:childsname,password:password},function(json)   {

    if(json.result === "success") {

        $("#add_err").html( "Welcome "+childsname+"!");

        setTimeout(function(){
              window.location= "menu.html"; 
        },2000);                          

    }else{
        $("#add_err").html(json.message);
    }
});
    }

脚本不起作用

$.post("includes/addVolunteer.inc.php",{volunteerName:volunteerName,volunteerCity:volunteerCity, volunteerCounty:volunteerCounty,volunteerService:volunteerService,volunteerEmail:volunteerEmail},function(json)   {

    if(json.result === "success") {

        $("#volunteerReport").html(json.message);

                setTimeout(function(){
              window.location= "view.htm"; 
        },2000); 


    }else{
        $("#volunteerReport").html(json.message);
    }
});
    }

我已经检查了控制台中的错误,但没有显示任何错误

最佳答案

你可以这样尝试

$.post("includes/addVolunteer.inc.php",{volunteerName:volunteerName,volunteerCity:volunteerCity, volunteerCounty:volunteerCounty,volunteerService:volunteerService,volunteerEmail:volunteerEmail},function(json)   {

    if(json[0] === "success") {

        $("#volunteerReport").html(json.message);

        setTimeout(function(){
            window.location= "view.htm"; 
        },2000); 


    }else{
        $("#volunteerReport").html(json.message);
    }
});
    }

我不确定,但对你有帮助。

关于javascript - setTimeout 不起作用,尽管可以在另一个类似的脚本中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30396844/

相关文章:

javascript - 如何在 Apple Instruments 中启用 Javascript 语法检查

javascript - 如何通过计算 d3.js 中的 `index` 值来添加动画延迟?

javascript - 消息提示 onsubmit return validate 但表单仍然提交

Javascript 运行时分析

jquery - 如何删除所有图像并添加新的图像列表?

javascript - jQuery "crop"分区

javascript - splitPinCodes.split (',' )给出错误

javascript - 如何使用 JQuery 获取 CSS 属性的值?

android - 任何设备上的背景视频,存在与否?

javascript - 我对这个函数有问题 "Uncaught ReferenceError: sync is not defined at HTMLInputElement.onkeyup"