'Creating Trigger using javascript and django

I want to make a trigger using javascript. This is my trigger in js :

setInterval(function(){
    $.ajax({
        type: 'GET',
        url : "{% url 'update_state' %}",
        success: function(response){
            
            console.log('Updated');

        },
        error: function(response){
            console.log('An error occured')
        }
    });
},60000);

And this is my view :

def update_state(request):
    requests = Request.objects.all()
    for req in requests:
        d = Request.objects.filter(id=req.id)
        dd = d.get()
        if dd.delay == timedelta(minutes=0):
            pass
        else:
            d_del = dd.delay - timedelta(minutes=1)
            d.update(delay=d_del)
    return HttpResponse('ok')

In my db sometimes the delay decrease but some time don't decrease. I don't know where is the problem.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source