首页 \ 问答 \ 每隔几分钟刷一下div - rails(Refresh div every few minutes - rails)

每隔几分钟刷一下div - rails(Refresh div every few minutes - rails)

我有一个页面的一部分需要每隔几分钟刷新一次,我之前在php中完成了这个并将其调整为以下rails:

:javascript
  $(document).ready(function() {
       setInterval(function() {
        $("#welcome2").load("floor7.haml #welcome2");
    }, 500);
  });

但是这在我的rails应用程序中不起作用,我不确定我是否正在输入正确的文件路径?

我明白了: ReferenceError: $ is not defined

更新

<body style="background: #000; font-family: 'Trebuchet MS'; overflow-x: hidden; margin:0;">
<script>
  //<![CDATA[
    $(document).ready(function() {
             setInterval(function() {
              $("#welcome2").load("/screen/floor7.haml #welcome2");
          }, 500);
    });
  //]]>
</script>
<div id='welcome2'>
  <span>Welcome</span>
</div>

I have part of a page that needs to be refreshed every few minutes, I've done this before in php and adjusted it to the following for rails:

:javascript
  $(document).ready(function() {
       setInterval(function() {
        $("#welcome2").load("floor7.haml #welcome2");
    }, 500);
  });

But this won't work in my rails application, I'm not sure if i'm putting the correct file path in?

I get this: ReferenceError: $ is not defined

Update

<body style="background: #000; font-family: 'Trebuchet MS'; overflow-x: hidden; margin:0;">
<script>
  //<![CDATA[
    $(document).ready(function() {
             setInterval(function() {
              $("#welcome2").load("/screen/floor7.haml #welcome2");
          }, 500);
    });
  //]]>
</script>
<div id='welcome2'>
  <span>Welcome</span>
</div>

原文:https://stackoverflow.com/questions/14214711
更新时间:2023-05-12 20:05

最满意答案

如何创建从fileInfos加载的可拖动/可调整大小的图像

根据你的fileInfos [i]调用一个创建group + image + anchors的函数:

    // pull info supplied by fileInfos for this “i”

    var imgWidth=fileInfos[i][1][0];
    var imgHeight=fileInfos[i][1][1];
    var groupX=fileInfos[i][2][0];
    var groupY=fileInfos[i][2][1];

    // call a function that creates the draggable/resizable group

    addImageGroup( images[i], imgWidth,imgHeight, groupX,groupY );

这是创建可拖动/可调整大小的组元素的函数:

  function addImageGroup(image,imageWidth,imageHeight,groupX,groupY){

      // width and height are based on the images width/height
      var w=imageWidth;
      var h=imageHeight;

      var kGroup = new Kinetic.Group({
          x:groupX,
          y:groupY,
          width:w+20,   // must allow 10+10=20 for anchors
          height:h+20,
          draggable:true
      });
      layer.add(kGroup);

      kGroup.on('dragstart', function() {
          this.moveToTop();
      });

      var kImage = new Kinetic.Image({
          x: 0,
          y: 0,
          image: image,
          width: w,
          height: h,
          name: 'image',
          stroke: 'black',
          strokeWidth: 2,
          dashArray: [10, 2]
      });
      kGroup.add(kImage);

      addAnchor(kGroup, 0, 0, 'topLeft');
      addAnchor(kGroup, w, 0, 'topRight');
      addAnchor(kGroup, w, h, 'bottomRight');
      addAnchor(kGroup, 0, h, 'bottomLeft');

  }

这是代码和小提琴: http//jsfiddle.net/m1erickson/buCzH/

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
      #container{
          border:1px solid red;
          width:350px;
          height:350px;
      }
    </style>
  </head>
  <body onmousedown="return false;">
    <div id="container"></div>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.2.min.js"></script>
    <script>

      // create the stage
      var stage = new Kinetic.Stage({
          container: 'container',
          width: 350,
          height: 350
      });
      var layer=new Kinetic.Layer();
      stage.add(layer);

      // build a test fileInfos array
      // width/height will be gotten from actual images, so leave width/height==0
      var fileInfos=[];
      function addFile(x,y,w,h,imgURL){
          fileInfos.push([imgURL,[w,h],[x,y]]);
      }
      addFile(30,100,102,102,"https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-1.jpg");
      addFile(200,100,102,102,"https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-3.jpg");

      // load all the images
      var images=[];
      loadAllImages();

      function loadAllImages(){
          var imagesOK=0;
          for (var i = 0; i < fileInfos.length; i++) {
              var img = new Image();
              images.push(img);
              img.onload = function(){ 
                  if (++imagesOK==fileInfos.length ) {

                      // all images are loaded, so build the groups
                      for(var i=0;i<fileInfos.length;i++){
                          var imgWidth=fileInfos[i][1][0];
                          var imgHeight=fileInfos[i][1][1];
                          var groupX=fileInfos[i][2][0];
                          var groupY=fileInfos[i][2][1];
                          addImageGroup( images[i], imgWidth,imgHeight, groupX,groupY );
                      }
                      layer.draw();
                  }
              }; 
              img.src = fileInfos[i][0];
          }      
      }


      function addImageGroup(image,imageWidth,imageHeight,groupX,groupY){

          // width and height are based on the images width/height
          var w=imageWidth;
          var h=imageHeight;

          var kGroup = new Kinetic.Group({
              x:groupX,
              y:groupY,
              width:w+20,   // must allow 10+10=20 for anchors
              height:h+20,
              draggable:true
          });
          layer.add(kGroup);

          kGroup.on('dragstart', function() {
              this.moveToTop();
          });

          var kImage = new Kinetic.Image({
              x: 0,
              y: 0,
              image: image,
              width: w,
              height: h,
              name: 'image',
              stroke: 'black',
              strokeWidth: 2,
              dashArray: [10, 2]
          });
          kGroup.add(kImage);

          addAnchor(kGroup, 0, 0, 'topLeft');
          addAnchor(kGroup, w, 0, 'topRight');
          addAnchor(kGroup, w, h, 'bottomRight');
          addAnchor(kGroup, 0, h, 'bottomLeft');

      }


      function update(activeAnchor) {
        var group = activeAnchor.getParent();

        var topLeft = group.get('.topLeft')[0];
        var topRight = group.get('.topRight')[0];
        var bottomRight = group.get('.bottomRight')[0];
        var bottomLeft = group.get('.bottomLeft')[0];
        var image = group.get('.image')[0];

        var anchorX = activeAnchor.getX();
        var anchorY = activeAnchor.getY();

        // update anchor positions
        switch (activeAnchor.getName()) {
          case 'topLeft':
            topRight.setY(anchorY);
            bottomLeft.setX(anchorX);
            break;
          case 'topRight':
            topLeft.setY(anchorY);
            bottomRight.setX(anchorX);
            break;
          case 'bottomRight':
            bottomLeft.setY(anchorY);
            topRight.setX(anchorX); 
            break;
          case 'bottomLeft':
            bottomRight.setY(anchorY);
            topLeft.setX(anchorX); 
            break;
        }

        image.setPosition(topLeft.getPosition());

        var width = topRight.getX() - topLeft.getX();
        var height = bottomLeft.getY() - topLeft.getY();
        if(width && height) {
          image.setSize(width, height);
        }
      }


      function addAnchor(group, x, y, name) {
        var stage = group.getStage();
        var layer = group.getLayer();

        var anchor = new Kinetic.Circle({
          x: x,
          y: y,
          stroke: '#666',
          fill: '#ddd',
          strokeWidth: 2,
          radius: 8,
          name: name,
          draggable: true,
          dragOnTop: false
        });

        anchor.on('dragmove', function() {
          update(this);
          layer.draw();
        });
        anchor.on('mousedown touchstart', function() {
          group.setDraggable(false);
          this.moveToTop();
        });
        anchor.on('dragend', function() {
          group.setDraggable(true);
          layer.draw();
        });
        // add hover styling
        anchor.on('mouseover', function() {
          var layer = this.getLayer();
          document.body.style.cursor = 'pointer';
          this.setStrokeWidth(4);
          layer.draw();
        });
        anchor.on('mouseout', function() {
          var layer = this.getLayer();
          document.body.style.cursor = 'default';
          this.setStrokeWidth(2);
          layer.draw();
        });

        group.add(anchor);
      }

    </script>

  </body>
</html>

How to create draggable/resizable images that are loaded from your fileInfos

Call a function that creates the group+image+anchors based on your fileInfos[i]:

    // pull info supplied by fileInfos for this “i”

    var imgWidth=fileInfos[i][1][0];
    var imgHeight=fileInfos[i][1][1];
    var groupX=fileInfos[i][2][0];
    var groupY=fileInfos[i][2][1];

    // call a function that creates the draggable/resizable group

    addImageGroup( images[i], imgWidth,imgHeight, groupX,groupY );

Here’s that function that creates the draggable/resizable group element:

  function addImageGroup(image,imageWidth,imageHeight,groupX,groupY){

      // width and height are based on the images width/height
      var w=imageWidth;
      var h=imageHeight;

      var kGroup = new Kinetic.Group({
          x:groupX,
          y:groupY,
          width:w+20,   // must allow 10+10=20 for anchors
          height:h+20,
          draggable:true
      });
      layer.add(kGroup);

      kGroup.on('dragstart', function() {
          this.moveToTop();
      });

      var kImage = new Kinetic.Image({
          x: 0,
          y: 0,
          image: image,
          width: w,
          height: h,
          name: 'image',
          stroke: 'black',
          strokeWidth: 2,
          dashArray: [10, 2]
      });
      kGroup.add(kImage);

      addAnchor(kGroup, 0, 0, 'topLeft');
      addAnchor(kGroup, w, 0, 'topRight');
      addAnchor(kGroup, w, h, 'bottomRight');
      addAnchor(kGroup, 0, h, 'bottomLeft');

  }

Here is code and a Fiddle: http://jsfiddle.net/m1erickson/buCzH/

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
      #container{
          border:1px solid red;
          width:350px;
          height:350px;
      }
    </style>
  </head>
  <body onmousedown="return false;">
    <div id="container"></div>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.2.min.js"></script>
    <script>

      // create the stage
      var stage = new Kinetic.Stage({
          container: 'container',
          width: 350,
          height: 350
      });
      var layer=new Kinetic.Layer();
      stage.add(layer);

      // build a test fileInfos array
      // width/height will be gotten from actual images, so leave width/height==0
      var fileInfos=[];
      function addFile(x,y,w,h,imgURL){
          fileInfos.push([imgURL,[w,h],[x,y]]);
      }
      addFile(30,100,102,102,"https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-1.jpg");
      addFile(200,100,102,102,"https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-3.jpg");

      // load all the images
      var images=[];
      loadAllImages();

      function loadAllImages(){
          var imagesOK=0;
          for (var i = 0; i < fileInfos.length; i++) {
              var img = new Image();
              images.push(img);
              img.onload = function(){ 
                  if (++imagesOK==fileInfos.length ) {

                      // all images are loaded, so build the groups
                      for(var i=0;i<fileInfos.length;i++){
                          var imgWidth=fileInfos[i][1][0];
                          var imgHeight=fileInfos[i][1][1];
                          var groupX=fileInfos[i][2][0];
                          var groupY=fileInfos[i][2][1];
                          addImageGroup( images[i], imgWidth,imgHeight, groupX,groupY );
                      }
                      layer.draw();
                  }
              }; 
              img.src = fileInfos[i][0];
          }      
      }


      function addImageGroup(image,imageWidth,imageHeight,groupX,groupY){

          // width and height are based on the images width/height
          var w=imageWidth;
          var h=imageHeight;

          var kGroup = new Kinetic.Group({
              x:groupX,
              y:groupY,
              width:w+20,   // must allow 10+10=20 for anchors
              height:h+20,
              draggable:true
          });
          layer.add(kGroup);

          kGroup.on('dragstart', function() {
              this.moveToTop();
          });

          var kImage = new Kinetic.Image({
              x: 0,
              y: 0,
              image: image,
              width: w,
              height: h,
              name: 'image',
              stroke: 'black',
              strokeWidth: 2,
              dashArray: [10, 2]
          });
          kGroup.add(kImage);

          addAnchor(kGroup, 0, 0, 'topLeft');
          addAnchor(kGroup, w, 0, 'topRight');
          addAnchor(kGroup, w, h, 'bottomRight');
          addAnchor(kGroup, 0, h, 'bottomLeft');

      }


      function update(activeAnchor) {
        var group = activeAnchor.getParent();

        var topLeft = group.get('.topLeft')[0];
        var topRight = group.get('.topRight')[0];
        var bottomRight = group.get('.bottomRight')[0];
        var bottomLeft = group.get('.bottomLeft')[0];
        var image = group.get('.image')[0];

        var anchorX = activeAnchor.getX();
        var anchorY = activeAnchor.getY();

        // update anchor positions
        switch (activeAnchor.getName()) {
          case 'topLeft':
            topRight.setY(anchorY);
            bottomLeft.setX(anchorX);
            break;
          case 'topRight':
            topLeft.setY(anchorY);
            bottomRight.setX(anchorX);
            break;
          case 'bottomRight':
            bottomLeft.setY(anchorY);
            topRight.setX(anchorX); 
            break;
          case 'bottomLeft':
            bottomRight.setY(anchorY);
            topLeft.setX(anchorX); 
            break;
        }

        image.setPosition(topLeft.getPosition());

        var width = topRight.getX() - topLeft.getX();
        var height = bottomLeft.getY() - topLeft.getY();
        if(width && height) {
          image.setSize(width, height);
        }
      }


      function addAnchor(group, x, y, name) {
        var stage = group.getStage();
        var layer = group.getLayer();

        var anchor = new Kinetic.Circle({
          x: x,
          y: y,
          stroke: '#666',
          fill: '#ddd',
          strokeWidth: 2,
          radius: 8,
          name: name,
          draggable: true,
          dragOnTop: false
        });

        anchor.on('dragmove', function() {
          update(this);
          layer.draw();
        });
        anchor.on('mousedown touchstart', function() {
          group.setDraggable(false);
          this.moveToTop();
        });
        anchor.on('dragend', function() {
          group.setDraggable(true);
          layer.draw();
        });
        // add hover styling
        anchor.on('mouseover', function() {
          var layer = this.getLayer();
          document.body.style.cursor = 'pointer';
          this.setStrokeWidth(4);
          layer.draw();
        });
        anchor.on('mouseout', function() {
          var layer = this.getLayer();
          document.body.style.cursor = 'default';
          this.setStrokeWidth(2);
          layer.draw();
        });

        group.add(anchor);
      }

    </script>

  </body>
</html>

相关问答

更多
  • 您可以使用UTF8的强大功能和• char var text = new Kinetic.Text({ fill : "black", text : "• item 1\n• item 2" }); 演示: http : //jsfiddle.net/lavrton/2QeM5/1/ 更新:您也可以使用html进行图像转换,然后您可以将图像添加到画布。 请参阅: http : //jsfiddle.net/lavrton/sDp6C/ You may use power of UTF8 w ...
  • 如何创建从fileInfos加载的可拖动/可调整大小的图像 根据你的fileInfos [i]调用一个创建group + image + anchors的函数: // pull info supplied by fileInfos for this “i” var imgWidth=fileInfos[i][1][0]; var imgHeight=fileInfos[i][1][1]; var groupX=fileInfos[i][2][0]; var grou ...
  • 我想我找到了解决方案。 尝试检测.on('resize')是,调整窗口大小会导致该事件触发数十亿次,从而拖动浏览器。 受这个答案的启发: 当使用JavaScript调整窗口大小时检测? - 我能够创建一个jQuery事件来检测(或多或少)调整大小的时间。 完成后,我清空容器元素并重新初始绘制脚本。 下面的代码可以做到这一点: $(window).on('resize',function(){ if(this.resizeTO) clearTimeout(this.resizeTO); thi ...
  • 好吧,根据@Cryptoburner的评论。 我在Kinetic中实现了一个自定义形状,并使用sceneFunc来使用相关链接中实现的drawImage函数。 var theImage = new Kinetic.Shape({ sceneFunc: function(context) { /// step 1 var oc = document.createElement('canvas'), octx = oc.getContext('2d ...
  • 请查看“海滩上的动物”实验室,该实验室是根据其坐标将形状物体卡入到位的示例: http://www.html5canvastutorials.com/labs/html5-canvas-animals-on-the-beach-game-with-kineticjs/ check out the "Animals on the Beach" lab which is an example of snapping shape objects into place based on their coordina ...
  • 尝试将KineticJS与AngularJS结合使用时,您会遇到一些集成问题 AngularJS擅长约束DOM元素。 但KineticJS对象不是DOM元素 - 它们只是画布上的像素。 所以AngularJS不能直接控制动力学对象。 为了让Kinetic对象响应文本输入更改而移动,可以使用AngularJS控制器并调用Kinetic对象的setX / setY。 要在拖动Kinetic对象时让文本输入值发生更改,可以从Kinetic dragmove事件处理函数中调用AngularJS控制器。 一个复杂的情 ...
  • 这是一个如何让Kinetic.Image在点击时报告XY的示例。 首先预加载所有图像 将click事件附加到每个新的Kinetic.Image 示例代码和演示: http : //jsfiddle.net/m1erickson/58a89/ Prototype