喵-见缝插针 2015-09-29 02:09 采纳率: 0%
浏览 44

AJAX或socket.io?

I am currently making a script, which should work as following: When you click a button the knob next to the button should insert a value. This have to be done in real time. So, now I would like to know what I should use for this. I researched a little and came up with ajax or socket.io, but which is better?

Some code:

<input class="knob" value="0" readonly data-width="80%">

<div class="button">
<a class="md-btn md-btn-success">Click me</a>
</div>

Animation for knob:

<input class="knob animated" value="0" readonly data-width="80%" rel="<?php echo $number; ?>">

<script>
       $('.knob').each(function () {

           var $this = $(this);
           var myVal = $this.attr("rel");
           $this.knob({});

           $({
               value: 0
           }).animate({

               value: myVal
           }, {
               duration: 2000,
               easing: 'swing',
               step: function () {
                   $this.val(Math.ceil(this.value)).trigger('change');

               }
           })

       });
</script>

I got the animation for the knob in another file and it works when I refresh, so I just have to get it into the real page.

  • 写回答

2条回答 默认 最新

  • weixin_33708432 2015-09-29 02:22
    关注

    Answer: Websockets.

    With clever ajax you can try to simulate real time. Otherwise websocket is the way to go.

    Based on one of the comments on my answer you may be interested in the Javascript framework socket.io

    From Mozilla

    WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

    See this example pulled from WebSocket that will definitely help you get started.

        var wsUri = "ws://echo.websocket.org/";
        var output;
    
        function init() {
            output = document.getElementById("output");
            testWebSocket();
        }
    
        function testWebSocket() {
            websocket = new WebSocket(wsUri);
            websocket.onopen = function(evt) {
                onOpen(evt)
            };
            websocket.onclose = function(evt) {
                onClose(evt)
            };
            websocket.onmessage = function(evt) {
                onMessage(evt)
            };
            websocket.onerror = function(evt) {
                onError(evt)
            };
        }
    
        function onOpen(evt) {
            writeToScreen("CONNECTED");
            doSend("WebSocket rocks");
        }
    
        function onClose(evt) {
            writeToScreen("DISCONNECTED");
        }
    
        function onMessage(evt) {
            writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
            websocket.close();
        }
    
        function onError(evt) {
            writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
        }
    
        function doSend(message) {
            writeToScreen("SENT: " + message);
            websocket.send(message);
        }
    
        function writeToScreen(message) {
            var pre = document.createElement("p");
            pre.style.wordWrap = "break-word";
            pre.innerHTML = message;
            output.appendChild(pre);
        }
        window.addEventListener("load", init, false);
    <h2>WebSocket Test</h2>
    <div id="output"></div>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题