• 长轮询


    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^index/',views.index),
        url(r'^get_json',views.get_json),
        url(r'^put_data',views.put_data),
    
    ]
    

      

    from django.shortcuts import render,HttpResponse
    import json
    from queue import Queue
    QUE= Queue()
    def index(requset):
        return render(requset,'index.html')
    def get_json(request):
        # data = {
        #     'status':True,
        #     'msg':'xxxx'
        # }
        response = {'status':True,'data':None}
        try:
            data =QUE.get(timeout=10)
            response ['data'] = data
        except Exception as ex:
            response['status'] =False
    
        return HttpResponse(json.dumps(response))
    def put_data(request):
        QUE.put('sa')
        return HttpResponse('扫码成功')
    

      

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title</title>
        <script src="/static/bs/js/jquery-3.1.1.js"></script>
    </head>
    <body>
    <h1 id="i1">内容空</h1>
    <script>
        $(function () {
           //setInterval(func,2000); 轮询
            func();
    
        });
        function  func() {
            $.ajax({
                url:'/get_json/',
                type:"POST",
                dataType:'JSON',
                success:function(arg){
                    if(arg.status){
                        location.href="https://www.baidu.com/"
                    }
                    else{
                        func();
                    }
    
    
                }
            })
    
        }
    </script>
    </body>
    </html>
  • 相关阅读:
    js判断是否为数字
    sublime3常用插件总结
    npm run build打包后自定义动画没有执行
    css实现未知元素宽高垂直居中和水平居中的方法
    vue3.0的设计目标
    chrome 80版本以后开启了SameSite
    http以及缓存、事件循环......
    vue移动端适配就这么玩
    前端性能监控
    能解决 80% 需求的 10个 CSS动画库
  • 原文地址:https://www.cnblogs.com/zhanglin123/p/9285349.html
Copyright © 2020-2023  润新知