Javascript 在不打开执行jquery ajax代码的网页的情况下执行jquery ajax代码

Javascript 在不打开执行jquery ajax代码的网页的情况下执行jquery ajax代码,javascript,php,html,jquery,laravel,Javascript,Php,Html,Jquery,Laravel,我和laravel 6做了一个项目 我的数据库表名是buildings id-木材-粘土-铁-作物-创建位置-更新位置 1-25-54-57-63-null-null 因此,我编写jqueryajax代码,将每列递增1作为测试 game.js $(文档).ready(函数(){ setInterval(函数(){ var dataString='id=1'; $.ajaxSetup({headers:{'X-CSRF-TOKEN':$('meta[name=“CSRF-TOKEN”]')).a

我和laravel 6做了一个项目 我的数据库表名是buildings id-木材-粘土-铁-作物-创建位置-更新位置 1-25-54-57-63-null-null 因此,我编写jqueryajax代码,将每列递增1作为测试

game.js

$(文档).ready(函数(){
setInterval(函数(){
var dataString='id=1';
$.ajaxSetup({headers:{'X-CSRF-TOKEN':$('meta[name=“CSRF-TOKEN”]')).attr('content')});
$.ajax({
类型:“post”,
url:“{route('upgradeBuildings')}}”,
数据:dataString,
cache:false,
beforeSend:function(){},
成功:功能(数据){
$('.x').html(数据);
},
错误:函数(err){console.log(err);}
});
}, 1000);

});这可以使用curl实现。例如:

curl --location --request POST 'http://yourhost.example/route/' --header 'X-CSRF-TOKEN:token' --data-raw 'yourdatahere'

PS:记住将url、令牌值和数据更改为要测试的实际值

您可以创建
Artisan
命令并运行
任务调度
诀窍在于每秒钟运行一次函数

可以使用以下变通方法:

<?php

namespace App\Console\Commands;
use Carbon\Carbon;
use Illuminate\Console\Command;

class upgradeBuilding extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'buildings:upgrade';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'run every seconds';


    /**
     * Create a new command instance.
     * 
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $count = 0;
        while ($count < 59) {
            $startTime =  Carbon::now();

            // Your logic here

            $endTime = Carbon::now();
            $totalDuration = $endTime->diffInSeconds($startTime);
            if($totalDuration > 0) {
                $count +=  $totalDuration;
            }
            else {
                $count++;
            }
            sleep(1);
        }

    }
}

您确实可以将此curl作为CRON作业的一部分来运行,但是使用Laravel Scheduler不是更好吗?当然。但OP从未提到它将每分钟/小时/天/等等运行。。。这只是实现它的N种方法之一的一个例子。非常正确,我只是假设基于setInterval 1000。无论如何,OP现在有很多选项可供选择。我想知道如何首先使用curl,那么您能告诉我如何通过curl提出此请求,以及如何安装curl并将此命令添加到其中吗。@Programatic这里有一个关于如何在windows中安装它的指南:如果您使用其他操作系统,internet上有很多指南。只需搜索2分钟。你能教我如何创建命令和计划吗?还有更多的解释吗please@Progromatic,这是非常完整和广泛的,您将有您的命令设置在任何时间
$schedule->command('buildings:upgrade')->everyMinute();