2017-10-11 161 views
0

我想在點擊打開新窗口:window.open打開無限開放循環

this.start = function(){ 
window.open("https://www.w3schools.com"); 
}; 

但它是無限循環,再而阿恩打開網頁。 我不知道代碼有什麼問題。

的完整代碼在這裏小提琴:https://jsfiddle.net/gbPGb/196/

+0

你應該在某個按鈕的'click'上觸發這個函數。似乎它在頁面加載時被調用,這就是爲什麼鏈開始並且不會結束。 –

+0

首先創建一個按鈕或鏈接,點擊該按鈕即可觸發事件。打開窗口' 然後編寫你的jquery '$(document)。(「click」,「#button」,function(){window.open(「https:// www.w3schools.com「); });' –

+0

可以在83行以某種方式完成嗎?我試過了,你可以在這裏看到:https://jsfiddle.net/gbPGb/197/但結果仍然是無限循環。 – user2295265

回答

0

我可能不是專家或完全理解JQuery的,但是我不知道如何共同sensely瞭解您的腳本。我也理解JavaScript,我已經檢查了你的jsfiddle。

(function(){ 
    /*HTML5 Stop Watch by Braden Best aka B1KMusic*/ 
    /*You can use this script anywhere and it will work*/ 
    var cvs,ctx,W,H,mem,StopWatch,Button,mouse; 
    mem = {}; 
    mouse = {x:-10,y:-10,down:false}; 
    cvs = document.createElement('canvas'); 
    cvs.width = W = 240; 
    cvs.height = H = 80; 
    (function appendCanvas(){ 
     if(document.body)document.body.appendChild(cvs); 
     else setTimeout(appendCanvas,100); 
    })(); 
    ctx = cvs.getContext('2d'); 
    function add(o){ 
     o.id = Math.floor(Math.random()*10000).toString(36); 
     for(var i in mem){ 
      if(mem.hasOwnProperty(i) && i == o.id){ 
       add(o); 
       return false; 
      } 
     } 
     mem[o.id] = o; 
    }; 
    function remove(o){ 
     delete mem[o.id]; 
    }; 
    function StopWatch(){ 
     var started = false, 
      time = [[0],[0,0],[0,0],[0,0]]; 
     this.run = function(){ 
      var output, 
       h = time[0], 
       m = time[1], 
       s = time[2], 
       ms = time[3]; 
      if(started){ 
       ms[1]++; 
       if(ms[1]>9){ms[1]=0;ms[0]++;} 
       if(ms[0]>9){ms[0]=0;s[1]++;} 
       if(s[1]>9){s[1]=0;s[0]++;} 
       if(s[0]>5){s[0]=0;m[1]++;} 
       if(m[1]>9){m[1]=0;m[0]++;} 
       if(m[0]>5){m[0]=0;h[0]++;} 
       if(h[0]>23){ms=[0,0];s=[0,0];m=[0,0];h[0]=0;} 
      } 
      ctx.font = 'bold 36px monospace'; 
      ctx.textAlign = 'center'; 
      ctx.textBaseline = 'middle'; 
      ctx.fillStyle = '#000'; 
      output = h[0]+':'+m[0]+m[1]+':'+s[0]+s[1]+'.'+ms[0]+ms[1]; 
      ctx.fillText(output,W/2,20); 
     }; 
     this.start = function(){ 

      if (started == false) { 
       started = true; 
       window.open("https://www.w3schools.com"); 
      } 
     }; 
     this.stop = function(){ 
      started = false; 
     }; 
     this.reset = function(){ 
      remove(this); 
      new StopWatch(); 
     } 
     add(this); 
    }; 
    function Button(x,y,t){ 
     var x = x, y = y, w = 60, h = 30, t = t; 
     this.run = function(){ 
      ctx.font = 'bold 16px monospace'; 
      ctx.textBaseline = 'middle'; 
      ctx.textAlign = 'center'; 
      ctx.beginPath();//begin mouse detection 
      ctx.rect(x,y,w,h); 
      if(ctx.isPointInPath(mouse.x,mouse.y)){ 
       ctx.fillStyle = '#eee'; 
       if(mouse.down){ 
        for(var i in mem){ 
         if(mem[i].constructor.name == 'StopWatch'){ 
          if(t == 'START')mem[i].start(); 
          if(t == 'STOP')mem[i].stop(); 
          if(t == 'RESET')mem[i].reset(); 
          if(t == 'CLOSE')document.body.removeChild(cvs); 
         } 
        } 
       } 
      }else{ 
       ctx.fillStyle = '#fff'; 
      } 
      ctx.closePath();//end mouse detection 
      ctx.fillRect(x,y,w,h); 
      ctx.fillStyle = '#000'; 
      ctx.fillText(t,x+w/2,y+h/2); 
     }; 
     add(this); 
    }; 
    (function init(){ 
     new StopWatch(); 
     new Button(0,50,'START'); 
     new Button(60,50,'STOP'); 
     new Button(120,50,'RESET'); 
     new Button(180,50,'CLOSE'); 
    })(); 
    (function loop(){ 
     var a 
     ctx.clearRect(0,0,W,H); 
     for(a in mem)if(mem.hasOwnProperty(a))mem[a].run(); 
     setTimeout(loop,1000/100); 
    })(); 
    cvs.onmousemove = function(e){ 
     mouse.x = (e.pageX||e.clientX||e.offsetX) - cvs.offsetLeft; 
     mouse.y = (e.pageY||e.clientY||e.offsetY) - cvs.offsetTop; 
     return false; 
    }; 
    cvs.onmousedown = cvs.onmouseup = function(e){ 
     mouse.down = e.type == 'mousedown'; 
     return false; 
    }; 
})(); 

從(看你的jsfiddle的第54行)

this.start = function(){ 
      started = true; 
      window.open("https://www.w3schools.com"); 
     }; 

要(添加if語句的功能)

this.start = function(){ 
       **if (started == false)** { 
       started = true; 
       window.open("https://www.w3schools.com"); 
       } 
      }; 

它運行後。它看起來很好,它只是打開你提供的地址的窗口。

+0

您能否將鏈接發佈到新的小提琴?我嘗試了你的建議,我仍然陷入無限循環。 – user2295265

+0

https://jsfiddle.net/a_jimwel/kt8L0rge/此處 –