setTimeout只在循环中运行一次

时间:2014-12-07 09:19:58

标签: javascript php jquery for-loop settimeout

我的目标是每X秒执行一次javascript函数。我已经读过应该使用setInterval,但我不相信我的情况。我也不想让变量必须重新发送,只是因为用户决定在所有内容正式发送之前更改内容

例如,我在时间上放了60,在数量上放了5,但它等了60秒,然后只发送了5个,每60秒发送1个文本,直到它发送了5个文本

我的javascript

    $('input[name="sendtxt"]').click(function(e) {
        sendText(e);
    });

function sendText(e) {
    e.preventDefault();
    var phonenum = $('input[name="phonenum"]').val();
    var provider = $('select[name="provider"]').val();
    var message = $('textarea[name="message"]').val();
    var otherprov = $('input[name="otherprov"]').val();
    var amount = $('input[name="amount"]').val();
    var time = $('input[name="time"]').val() * 1000;
    var sendmsg;
    for (sendmsg = 1; sendmsg <= amount; sendmsg++) {
        setTimeout(function() {
            $.ajax({
                type: 'POST',
                data: {
                    provider: provider,
                    message: message,
                    phonenum: phonenum,
                    amount: amount,
                    otherprov: otherprov,
                    time: time
                },
                url: 'send.php',
                success: function(data) {
                    console.log('Success. Sent ' + sendmsg + " texts.");
                },
                error: function(xhr, err) {
                    console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                    console.log("responseText: " + xhr.responseText);
                }
            });
        }, time);
    }

和我的PHP

<?php


$to = $_POST["phonenum"];
$provider = $_POST["provider"];

$otherprov = $_POST["otherprov"];
$fixedotherprov = str_replace("otherprovider", "", $otherprov);


$completenum = $to . $provider . $fixedotherprov;
$completenum = str_replace('otherprovider', '', $completenum);
$subject = 'Hello';
$message = $_POST["message"];
$headers = 'From: Daniel';
$amount = $_POST["amount"];

//for ($x = 0; $x < $amount; $x++) {
    mail($completenum, $subject, $message, $headers);
//}

?>

2 个答案:

答案 0 :(得分:0)

setTimeout是一个异步函数。超时事件在您从sendmsg = 1sendmsg <= amount的循环中定义。执行for循环后,正在执行for循环下面的其余代码,直到超时事件触发。触发超时事件时,sendmsg的值等于amount+1,因为循环已经完成执行。因此,所有超时事件都会将sendmsg的值设为amout+1,从而导致意外行为。

在下面的代码中,每次迭代都会定义一个临时范围,为每个sendmsg事件提供不同的timeout值。

您还必须将不同的时间安排为每次迭代中的超时时间值,以便在不同的超时中执行它们。

像这样修改你的代码..

function sendText(e) {
    e.preventDefault();
    var phonenum = $('input[name="phonenum"]').val();
    var provider = $('select[name="provider"]').val();
    var message = $('textarea[name="message"]').val();
    var otherprov = $('input[name="otherprov"]').val();
    var amount = $('input[name="amount"]').val();
    var time = $('input[name="time"]').val();
    var sendmsg;
    for (sendmsg = 1; sendmsg <= amount; sendmsg++) {
            (function(sendmsg){
               setTimeout(function() {
                $.ajax({
                    type: 'POST',
                    data: {
                        provider: provider,
                        message: message,
                        phonenum: phonenum,
                        amount: amount,
                        otherprov: otherprov,
                        time: time * 1000 * sendmsg
                    },
                    url: 'send.php',
                    success: function(data) {
                        console.log('Success. Sent ' + sendmsg + " texts.");
                    },
                    error: function(xhr, err) {
                        console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                        console.log("responseText: " + xhr.responseText);
                    }
                });
            }, time * 1000 * sendmsg);
           })(sendmsg);
        }
}

答案 1 :(得分:0)

这是一个范围问题。

详细了解Javascript闭包(作用域)函数:http://brackets.clementng.me/post/24150213014/example-of-a-javascript-closure-settimeout-inside

将您的代码更改为:

for (sendmsg = 1; sendmsg <= amount; sendmsg++) {
    (function(){
        setTimeout(function(_provider, _message, _phonenum, _amount, _otherprov, _time, _sendmsg) {
            $.ajax({
                type: 'POST',
                data: {
                    provider: _provider,
                    message: _message,
                    phonenum: _phonenum,
                    amount: _amount,
                    otherprov: _otherprov,
                    time: _time
                },
                url: 'send.php',
                success: function(data) {
                    console.log('Success. Sent ' + _sendmsg + " texts.");
                },
                error: function(xhr, err) {
                    console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                    console.log("responseText: " + xhr.responseText);
                }
            });
        }, time);
    })(provider, message, phonenum, amount, otherprov, time, sendmsg);
}
希望有所帮助。